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