pdc202xx_new.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Promise TX2/TX4/TX2000/133 IDE driver
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Split from:
  10. * linux/drivers/ide/pdc202xx.c Version 0.35 Mar. 30, 2002
  11. * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>
  12. * Copyright (C) 2005-2007 MontaVista Software, Inc.
  13. * Portions Copyright (C) 1999 Promise Technology, Inc.
  14. * Author: Frank Tiernan (frankt@promise.com)
  15. * Released under terms of General Public License
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/pci.h>
  22. #include <linux/init.h>
  23. #include <linux/ide.h>
  24. #include <linux/ktime.h>
  25. #include <asm/io.h>
  26. #ifdef CONFIG_PPC_PMAC
  27. #include <asm/prom.h>
  28. #include <asm/pci-bridge.h>
  29. #endif
  30. #define DRV_NAME "pdc202xx_new"
  31. #undef DEBUG
  32. #ifdef DEBUG
  33. #define DBG(fmt, args...) printk("%s: " fmt, __func__, ## args)
  34. #else
  35. #define DBG(fmt, args...)
  36. #endif
  37. static u8 max_dma_rate(struct pci_dev *pdev)
  38. {
  39. u8 mode;
  40. switch(pdev->device) {
  41. case PCI_DEVICE_ID_PROMISE_20277:
  42. case PCI_DEVICE_ID_PROMISE_20276:
  43. case PCI_DEVICE_ID_PROMISE_20275:
  44. case PCI_DEVICE_ID_PROMISE_20271:
  45. case PCI_DEVICE_ID_PROMISE_20269:
  46. mode = 4;
  47. break;
  48. case PCI_DEVICE_ID_PROMISE_20270:
  49. case PCI_DEVICE_ID_PROMISE_20268:
  50. mode = 3;
  51. break;
  52. default:
  53. return 0;
  54. }
  55. return mode;
  56. }
  57. /**
  58. * get_indexed_reg - Get indexed register
  59. * @hwif: for the port address
  60. * @index: index of the indexed register
  61. */
  62. static u8 get_indexed_reg(ide_hwif_t *hwif, u8 index)
  63. {
  64. u8 value;
  65. outb(index, hwif->dma_base + 1);
  66. value = inb(hwif->dma_base + 3);
  67. DBG("index[%02X] value[%02X]\n", index, value);
  68. return value;
  69. }
  70. /**
  71. * set_indexed_reg - Set indexed register
  72. * @hwif: for the port address
  73. * @index: index of the indexed register
  74. */
  75. static void set_indexed_reg(ide_hwif_t *hwif, u8 index, u8 value)
  76. {
  77. outb(index, hwif->dma_base + 1);
  78. outb(value, hwif->dma_base + 3);
  79. DBG("index[%02X] value[%02X]\n", index, value);
  80. }
  81. /*
  82. * ATA Timing Tables based on 133 MHz PLL output clock.
  83. *
  84. * If the PLL outputs 100 MHz clock, the ASIC hardware will set
  85. * the timing registers automatically when "set features" command is
  86. * issued to the device. However, if the PLL output clock is 133 MHz,
  87. * the following tables must be used.
  88. */
  89. static struct pio_timing {
  90. u8 reg0c, reg0d, reg13;
  91. } pio_timings [] = {
  92. { 0xfb, 0x2b, 0xac }, /* PIO mode 0, IORDY off, Prefetch off */
  93. { 0x46, 0x29, 0xa4 }, /* PIO mode 1, IORDY off, Prefetch off */
  94. { 0x23, 0x26, 0x64 }, /* PIO mode 2, IORDY off, Prefetch off */
  95. { 0x27, 0x0d, 0x35 }, /* PIO mode 3, IORDY on, Prefetch off */
  96. { 0x23, 0x09, 0x25 }, /* PIO mode 4, IORDY on, Prefetch off */
  97. };
  98. static struct mwdma_timing {
  99. u8 reg0e, reg0f;
  100. } mwdma_timings [] = {
  101. { 0xdf, 0x5f }, /* MWDMA mode 0 */
  102. { 0x6b, 0x27 }, /* MWDMA mode 1 */
  103. { 0x69, 0x25 }, /* MWDMA mode 2 */
  104. };
  105. static struct udma_timing {
  106. u8 reg10, reg11, reg12;
  107. } udma_timings [] = {
  108. { 0x4a, 0x0f, 0xd5 }, /* UDMA mode 0 */
  109. { 0x3a, 0x0a, 0xd0 }, /* UDMA mode 1 */
  110. { 0x2a, 0x07, 0xcd }, /* UDMA mode 2 */
  111. { 0x1a, 0x05, 0xcd }, /* UDMA mode 3 */
  112. { 0x1a, 0x03, 0xcd }, /* UDMA mode 4 */
  113. { 0x1a, 0x02, 0xcb }, /* UDMA mode 5 */
  114. { 0x1a, 0x01, 0xcb }, /* UDMA mode 6 */
  115. };
  116. static void pdcnew_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  117. {
  118. struct pci_dev *dev = to_pci_dev(hwif->dev);
  119. u8 adj = (drive->dn & 1) ? 0x08 : 0x00;
  120. const u8 speed = drive->dma_mode;
  121. /*
  122. * IDE core issues SETFEATURES_XFER to the drive first (thanks to
  123. * IDE_HFLAG_POST_SET_MODE in ->host_flags). PDC202xx hardware will
  124. * automatically set the timing registers based on 100 MHz PLL output.
  125. *
  126. * As we set up the PLL to output 133 MHz for UltraDMA/133 capable
  127. * chips, we must override the default register settings...
  128. */
  129. if (max_dma_rate(dev) == 4) {
  130. u8 mode = speed & 0x07;
  131. if (speed >= XFER_UDMA_0) {
  132. set_indexed_reg(hwif, 0x10 + adj,
  133. udma_timings[mode].reg10);
  134. set_indexed_reg(hwif, 0x11 + adj,
  135. udma_timings[mode].reg11);
  136. set_indexed_reg(hwif, 0x12 + adj,
  137. udma_timings[mode].reg12);
  138. } else {
  139. set_indexed_reg(hwif, 0x0e + adj,
  140. mwdma_timings[mode].reg0e);
  141. set_indexed_reg(hwif, 0x0f + adj,
  142. mwdma_timings[mode].reg0f);
  143. }
  144. } else if (speed == XFER_UDMA_2) {
  145. /* Set tHOLD bit to 0 if using UDMA mode 2 */
  146. u8 tmp = get_indexed_reg(hwif, 0x10 + adj);
  147. set_indexed_reg(hwif, 0x10 + adj, tmp & 0x7f);
  148. }
  149. }
  150. static void pdcnew_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  151. {
  152. struct pci_dev *dev = to_pci_dev(hwif->dev);
  153. u8 adj = (drive->dn & 1) ? 0x08 : 0x00;
  154. const u8 pio = drive->pio_mode - XFER_PIO_0;
  155. if (max_dma_rate(dev) == 4) {
  156. set_indexed_reg(hwif, 0x0c + adj, pio_timings[pio].reg0c);
  157. set_indexed_reg(hwif, 0x0d + adj, pio_timings[pio].reg0d);
  158. set_indexed_reg(hwif, 0x13 + adj, pio_timings[pio].reg13);
  159. }
  160. }
  161. static u8 pdcnew_cable_detect(ide_hwif_t *hwif)
  162. {
  163. if (get_indexed_reg(hwif, 0x0b) & 0x04)
  164. return ATA_CBL_PATA40;
  165. else
  166. return ATA_CBL_PATA80;
  167. }
  168. static void pdcnew_reset(ide_drive_t *drive)
  169. {
  170. /*
  171. * Deleted this because it is redundant from the caller.
  172. */
  173. printk(KERN_WARNING "pdc202xx_new: %s channel reset.\n",
  174. drive->hwif->channel ? "Secondary" : "Primary");
  175. }
  176. /**
  177. * read_counter - Read the byte count registers
  178. * @dma_base: for the port address
  179. */
  180. static long read_counter(u32 dma_base)
  181. {
  182. u32 pri_dma_base = dma_base, sec_dma_base = dma_base + 0x08;
  183. u8 cnt0, cnt1, cnt2, cnt3;
  184. long count = 0, last;
  185. int retry = 3;
  186. do {
  187. last = count;
  188. /* Read the current count */
  189. outb(0x20, pri_dma_base + 0x01);
  190. cnt0 = inb(pri_dma_base + 0x03);
  191. outb(0x21, pri_dma_base + 0x01);
  192. cnt1 = inb(pri_dma_base + 0x03);
  193. outb(0x20, sec_dma_base + 0x01);
  194. cnt2 = inb(sec_dma_base + 0x03);
  195. outb(0x21, sec_dma_base + 0x01);
  196. cnt3 = inb(sec_dma_base + 0x03);
  197. count = (cnt3 << 23) | (cnt2 << 15) | (cnt1 << 8) | cnt0;
  198. /*
  199. * The 30-bit decrementing counter is read in 4 pieces.
  200. * Incorrect value may be read when the most significant bytes
  201. * are changing...
  202. */
  203. } while (retry-- && (((last ^ count) & 0x3fff8000) || last < count));
  204. DBG("cnt0[%02X] cnt1[%02X] cnt2[%02X] cnt3[%02X]\n",
  205. cnt0, cnt1, cnt2, cnt3);
  206. return count;
  207. }
  208. /**
  209. * detect_pll_input_clock - Detect the PLL input clock in Hz.
  210. * @dma_base: for the port address
  211. * E.g. 16949000 on 33 MHz PCI bus, i.e. half of the PCI clock.
  212. */
  213. static long detect_pll_input_clock(unsigned long dma_base)
  214. {
  215. ktime_t start_time, end_time;
  216. long start_count, end_count;
  217. long pll_input, usec_elapsed;
  218. u8 scr1;
  219. start_count = read_counter(dma_base);
  220. start_time = ktime_get();
  221. /* Start the test mode */
  222. outb(0x01, dma_base + 0x01);
  223. scr1 = inb(dma_base + 0x03);
  224. DBG("scr1[%02X]\n", scr1);
  225. outb(scr1 | 0x40, dma_base + 0x03);
  226. /* Let the counter run for 10 ms. */
  227. mdelay(10);
  228. end_count = read_counter(dma_base);
  229. end_time = ktime_get();
  230. /* Stop the test mode */
  231. outb(0x01, dma_base + 0x01);
  232. scr1 = inb(dma_base + 0x03);
  233. DBG("scr1[%02X]\n", scr1);
  234. outb(scr1 & ~0x40, dma_base + 0x03);
  235. /*
  236. * Calculate the input clock in Hz
  237. * (the clock counter is 30 bit wide and counts down)
  238. */
  239. usec_elapsed = ktime_us_delta(end_time, start_time);
  240. pll_input = ((start_count - end_count) & 0x3fffffff) / 10 *
  241. (10000000 / usec_elapsed);
  242. DBG("start[%ld] end[%ld]\n", start_count, end_count);
  243. return pll_input;
  244. }
  245. #ifdef CONFIG_PPC_PMAC
  246. static void apple_kiwi_init(struct pci_dev *pdev)
  247. {
  248. struct device_node *np = pci_device_to_OF_node(pdev);
  249. u8 conf;
  250. if (np == NULL || !of_device_is_compatible(np, "kiwi-root"))
  251. return;
  252. if (pdev->revision >= 0x03) {
  253. /* Setup chip magic config stuff (from darwin) */
  254. pci_read_config_byte (pdev, 0x40, &conf);
  255. pci_write_config_byte(pdev, 0x40, (conf | 0x01));
  256. }
  257. }
  258. #endif /* CONFIG_PPC_PMAC */
  259. static int init_chipset_pdcnew(struct pci_dev *dev)
  260. {
  261. const char *name = DRV_NAME;
  262. unsigned long dma_base = pci_resource_start(dev, 4);
  263. unsigned long sec_dma_base = dma_base + 0x08;
  264. long pll_input, pll_output, ratio;
  265. int f, r;
  266. u8 pll_ctl0, pll_ctl1;
  267. if (dma_base == 0)
  268. return -EFAULT;
  269. #ifdef CONFIG_PPC_PMAC
  270. apple_kiwi_init(dev);
  271. #endif
  272. /* Calculate the required PLL output frequency */
  273. switch(max_dma_rate(dev)) {
  274. case 4: /* it's 133 MHz for Ultra133 chips */
  275. pll_output = 133333333;
  276. break;
  277. case 3: /* and 100 MHz for Ultra100 chips */
  278. default:
  279. pll_output = 100000000;
  280. break;
  281. }
  282. /*
  283. * Detect PLL input clock.
  284. * On some systems, where PCI bus is running at non-standard clock rate
  285. * (e.g. 25 or 40 MHz), we have to adjust the cycle time.
  286. * PDC20268 and newer chips employ PLL circuit to help correct timing
  287. * registers setting.
  288. */
  289. pll_input = detect_pll_input_clock(dma_base);
  290. printk(KERN_INFO "%s %s: PLL input clock is %ld kHz\n",
  291. name, pci_name(dev), pll_input / 1000);
  292. /* Sanity check */
  293. if (unlikely(pll_input < 5000000L || pll_input > 70000000L)) {
  294. printk(KERN_ERR "%s %s: Bad PLL input clock %ld Hz, giving up!"
  295. "\n", name, pci_name(dev), pll_input);
  296. goto out;
  297. }
  298. #ifdef DEBUG
  299. DBG("pll_output is %ld Hz\n", pll_output);
  300. /* Show the current clock value of PLL control register
  301. * (maybe already configured by the BIOS)
  302. */
  303. outb(0x02, sec_dma_base + 0x01);
  304. pll_ctl0 = inb(sec_dma_base + 0x03);
  305. outb(0x03, sec_dma_base + 0x01);
  306. pll_ctl1 = inb(sec_dma_base + 0x03);
  307. DBG("pll_ctl[%02X][%02X]\n", pll_ctl0, pll_ctl1);
  308. #endif
  309. /*
  310. * Calculate the ratio of F, R and NO
  311. * POUT = (F + 2) / (( R + 2) * NO)
  312. */
  313. ratio = pll_output / (pll_input / 1000);
  314. if (ratio < 8600L) { /* 8.6x */
  315. /* Using NO = 0x01, R = 0x0d */
  316. r = 0x0d;
  317. } else if (ratio < 12900L) { /* 12.9x */
  318. /* Using NO = 0x01, R = 0x08 */
  319. r = 0x08;
  320. } else if (ratio < 16100L) { /* 16.1x */
  321. /* Using NO = 0x01, R = 0x06 */
  322. r = 0x06;
  323. } else if (ratio < 64000L) { /* 64x */
  324. r = 0x00;
  325. } else {
  326. /* Invalid ratio */
  327. printk(KERN_ERR "%s %s: Bad ratio %ld, giving up!\n",
  328. name, pci_name(dev), ratio);
  329. goto out;
  330. }
  331. f = (ratio * (r + 2)) / 1000 - 2;
  332. DBG("F[%d] R[%d] ratio*1000[%ld]\n", f, r, ratio);
  333. if (unlikely(f < 0 || f > 127)) {
  334. /* Invalid F */
  335. printk(KERN_ERR "%s %s: F[%d] invalid!\n",
  336. name, pci_name(dev), f);
  337. goto out;
  338. }
  339. pll_ctl0 = (u8) f;
  340. pll_ctl1 = (u8) r;
  341. DBG("Writing pll_ctl[%02X][%02X]\n", pll_ctl0, pll_ctl1);
  342. outb(0x02, sec_dma_base + 0x01);
  343. outb(pll_ctl0, sec_dma_base + 0x03);
  344. outb(0x03, sec_dma_base + 0x01);
  345. outb(pll_ctl1, sec_dma_base + 0x03);
  346. /* Wait the PLL circuit to be stable */
  347. mdelay(30);
  348. #ifdef DEBUG
  349. /*
  350. * Show the current clock value of PLL control register
  351. */
  352. outb(0x02, sec_dma_base + 0x01);
  353. pll_ctl0 = inb(sec_dma_base + 0x03);
  354. outb(0x03, sec_dma_base + 0x01);
  355. pll_ctl1 = inb(sec_dma_base + 0x03);
  356. DBG("pll_ctl[%02X][%02X]\n", pll_ctl0, pll_ctl1);
  357. #endif
  358. out:
  359. return 0;
  360. }
  361. static struct pci_dev *pdc20270_get_dev2(struct pci_dev *dev)
  362. {
  363. struct pci_dev *dev2;
  364. dev2 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn) + 1,
  365. PCI_FUNC(dev->devfn)));
  366. if (dev2 &&
  367. dev2->vendor == dev->vendor &&
  368. dev2->device == dev->device) {
  369. if (dev2->irq != dev->irq) {
  370. dev2->irq = dev->irq;
  371. printk(KERN_INFO DRV_NAME " %s: PCI config space "
  372. "interrupt fixed\n", pci_name(dev));
  373. }
  374. return dev2;
  375. }
  376. return NULL;
  377. }
  378. static const struct ide_port_ops pdcnew_port_ops = {
  379. .set_pio_mode = pdcnew_set_pio_mode,
  380. .set_dma_mode = pdcnew_set_dma_mode,
  381. .resetproc = pdcnew_reset,
  382. .cable_detect = pdcnew_cable_detect,
  383. };
  384. #define DECLARE_PDCNEW_DEV(udma) \
  385. { \
  386. .name = DRV_NAME, \
  387. .init_chipset = init_chipset_pdcnew, \
  388. .port_ops = &pdcnew_port_ops, \
  389. .host_flags = IDE_HFLAG_POST_SET_MODE | \
  390. IDE_HFLAG_ERROR_STOPS_FIFO | \
  391. IDE_HFLAG_OFF_BOARD, \
  392. .pio_mask = ATA_PIO4, \
  393. .mwdma_mask = ATA_MWDMA2, \
  394. .udma_mask = udma, \
  395. }
  396. static const struct ide_port_info pdcnew_chipsets[] = {
  397. /* 0: PDC202{68,70} */ DECLARE_PDCNEW_DEV(ATA_UDMA5),
  398. /* 1: PDC202{69,71,75,76,77} */ DECLARE_PDCNEW_DEV(ATA_UDMA6),
  399. };
  400. /**
  401. * pdc202new_init_one - called when a pdc202xx is found
  402. * @dev: the pdc202new device
  403. * @id: the matching pci id
  404. *
  405. * Called when the PCI registration layer (or the IDE initialization)
  406. * finds a device matching our IDE device tables.
  407. */
  408. static int pdc202new_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  409. {
  410. const struct ide_port_info *d = &pdcnew_chipsets[id->driver_data];
  411. struct pci_dev *bridge = dev->bus->self;
  412. if (dev->device == PCI_DEVICE_ID_PROMISE_20270 && bridge &&
  413. bridge->vendor == PCI_VENDOR_ID_DEC &&
  414. bridge->device == PCI_DEVICE_ID_DEC_21150) {
  415. struct pci_dev *dev2;
  416. if (PCI_SLOT(dev->devfn) & 2)
  417. return -ENODEV;
  418. dev2 = pdc20270_get_dev2(dev);
  419. if (dev2) {
  420. int ret = ide_pci_init_two(dev, dev2, d, NULL);
  421. if (ret < 0)
  422. pci_dev_put(dev2);
  423. return ret;
  424. }
  425. }
  426. if (dev->device == PCI_DEVICE_ID_PROMISE_20276 && bridge &&
  427. bridge->vendor == PCI_VENDOR_ID_INTEL &&
  428. (bridge->device == PCI_DEVICE_ID_INTEL_I960 ||
  429. bridge->device == PCI_DEVICE_ID_INTEL_I960RM)) {
  430. printk(KERN_INFO DRV_NAME " %s: attached to I2O RAID controller,"
  431. " skipping\n", pci_name(dev));
  432. return -ENODEV;
  433. }
  434. return ide_pci_init_one(dev, d, NULL);
  435. }
  436. static void pdc202new_remove(struct pci_dev *dev)
  437. {
  438. struct ide_host *host = pci_get_drvdata(dev);
  439. struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
  440. ide_pci_remove(dev);
  441. pci_dev_put(dev2);
  442. }
  443. static const struct pci_device_id pdc202new_pci_tbl[] = {
  444. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20268), 0 },
  445. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20269), 1 },
  446. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20270), 0 },
  447. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20271), 1 },
  448. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20275), 1 },
  449. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20276), 1 },
  450. { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20277), 1 },
  451. { 0, },
  452. };
  453. MODULE_DEVICE_TABLE(pci, pdc202new_pci_tbl);
  454. static struct pci_driver pdc202new_pci_driver = {
  455. .name = "Promise_IDE",
  456. .id_table = pdc202new_pci_tbl,
  457. .probe = pdc202new_init_one,
  458. .remove = pdc202new_remove,
  459. .suspend = ide_pci_suspend,
  460. .resume = ide_pci_resume,
  461. };
  462. static int __init pdc202new_ide_init(void)
  463. {
  464. return ide_pci_register_driver(&pdc202new_pci_driver);
  465. }
  466. static void __exit pdc202new_ide_exit(void)
  467. {
  468. pci_unregister_driver(&pdc202new_pci_driver);
  469. }
  470. module_init(pdc202new_ide_init);
  471. module_exit(pdc202new_ide_exit);
  472. MODULE_AUTHOR("Andre Hedrick, Frank Tiernan");
  473. MODULE_DESCRIPTION("PCI driver module for Promise PDC20268 and higher");
  474. MODULE_LICENSE("GPL");