alim15x3.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (C) 1998-2000 Michel Aubry, Maintainer
  3. * Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
  4. * Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
  5. *
  6. * Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
  7. * May be copied or modified under the terms of the GNU General Public License
  8. * Copyright (C) 2002 Alan Cox <alan@redhat.com>
  9. * ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
  10. * Copyright (C) 2007 MontaVista Software, Inc. <source@mvista.com>
  11. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
  12. *
  13. * (U)DMA capable version of ali 1533/1543(C), 1535(D)
  14. *
  15. **********************************************************************
  16. * 9/7/99 --Parts from the above author are included and need to be
  17. * converted into standard interface, once I finish the thought.
  18. *
  19. * Recent changes
  20. * Don't use LBA48 mode on ALi <= 0xC4
  21. * Don't poke 0x79 with a non ALi northbridge
  22. * Don't flip undefined bits on newer chipsets (fix Fujitsu laptop hang)
  23. * Allow UDMA6 on revisions > 0xC4
  24. *
  25. * Documentation
  26. * Chipset documentation available under NDA only
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/pci.h>
  33. #include <linux/hdreg.h>
  34. #include <linux/ide.h>
  35. #include <linux/init.h>
  36. #include <linux/dmi.h>
  37. #include <asm/io.h>
  38. /*
  39. * ALi devices are not plug in. Otherwise these static values would
  40. * need to go. They ought to go away anyway
  41. */
  42. static u8 m5229_revision;
  43. static u8 chip_is_1543c_e;
  44. static struct pci_dev *isa_dev;
  45. /**
  46. * ali_set_pio_mode - set host controller for PIO mode
  47. * @drive: drive
  48. * @pio: PIO mode number
  49. *
  50. * Program the controller for the given PIO mode.
  51. */
  52. static void ali_set_pio_mode(ide_drive_t *drive, const u8 pio)
  53. {
  54. ide_hwif_t *hwif = HWIF(drive);
  55. struct pci_dev *dev = to_pci_dev(hwif->dev);
  56. int s_time, a_time, c_time;
  57. u8 s_clc, a_clc, r_clc;
  58. unsigned long flags;
  59. int bus_speed = ide_pci_clk ? ide_pci_clk : system_bus_clock();
  60. int port = hwif->channel ? 0x5c : 0x58;
  61. int portFIFO = hwif->channel ? 0x55 : 0x54;
  62. u8 cd_dma_fifo = 0;
  63. int unit = drive->select.b.unit & 1;
  64. s_time = ide_pio_timings[pio].setup_time;
  65. a_time = ide_pio_timings[pio].active_time;
  66. if ((s_clc = (s_time * bus_speed + 999) / 1000) >= 8)
  67. s_clc = 0;
  68. if ((a_clc = (a_time * bus_speed + 999) / 1000) >= 8)
  69. a_clc = 0;
  70. c_time = ide_pio_timings[pio].cycle_time;
  71. #if 0
  72. if ((r_clc = ((c_time - s_time - a_time) * bus_speed + 999) / 1000) >= 16)
  73. r_clc = 0;
  74. #endif
  75. if (!(r_clc = (c_time * bus_speed + 999) / 1000 - a_clc - s_clc)) {
  76. r_clc = 1;
  77. } else {
  78. if (r_clc >= 16)
  79. r_clc = 0;
  80. }
  81. local_irq_save(flags);
  82. /*
  83. * PIO mode => ATA FIFO on, ATAPI FIFO off
  84. */
  85. pci_read_config_byte(dev, portFIFO, &cd_dma_fifo);
  86. if (drive->media==ide_disk) {
  87. if (unit) {
  88. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0x0F) | 0x50);
  89. } else {
  90. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0xF0) | 0x05);
  91. }
  92. } else {
  93. if (unit) {
  94. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0x0F);
  95. } else {
  96. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0xF0);
  97. }
  98. }
  99. pci_write_config_byte(dev, port, s_clc);
  100. pci_write_config_byte(dev, port+drive->select.b.unit+2, (a_clc << 4) | r_clc);
  101. local_irq_restore(flags);
  102. /*
  103. * setup active rec
  104. * { 70, 165, 365 }, PIO Mode 0
  105. * { 50, 125, 208 }, PIO Mode 1
  106. * { 30, 100, 110 }, PIO Mode 2
  107. * { 30, 80, 70 }, PIO Mode 3 with IORDY
  108. * { 25, 70, 25 }, PIO Mode 4 with IORDY ns
  109. * { 20, 50, 30 } PIO Mode 5 with IORDY (nonstandard)
  110. */
  111. }
  112. /**
  113. * ali_udma_filter - compute UDMA mask
  114. * @drive: IDE device
  115. *
  116. * Return available UDMA modes.
  117. *
  118. * The actual rules for the ALi are:
  119. * No UDMA on revisions <= 0x20
  120. * Disk only for revisions < 0xC2
  121. * Not WDC drives for revisions < 0xC2
  122. *
  123. * FIXME: WDC ifdef needs to die
  124. */
  125. static u8 ali_udma_filter(ide_drive_t *drive)
  126. {
  127. if (m5229_revision > 0x20 && m5229_revision < 0xC2) {
  128. if (drive->media != ide_disk)
  129. return 0;
  130. #ifndef CONFIG_WDC_ALI15X3
  131. if (chip_is_1543c_e && strstr(drive->id->model, "WDC "))
  132. return 0;
  133. #endif
  134. }
  135. return drive->hwif->ultra_mask;
  136. }
  137. /**
  138. * ali_set_dma_mode - set host controller for DMA mode
  139. * @drive: drive
  140. * @speed: DMA mode
  141. *
  142. * Configure the hardware for the desired IDE transfer mode.
  143. */
  144. static void ali_set_dma_mode(ide_drive_t *drive, const u8 speed)
  145. {
  146. ide_hwif_t *hwif = HWIF(drive);
  147. struct pci_dev *dev = to_pci_dev(hwif->dev);
  148. u8 speed1 = speed;
  149. u8 unit = (drive->select.b.unit & 0x01);
  150. u8 tmpbyte = 0x00;
  151. int m5229_udma = (hwif->channel) ? 0x57 : 0x56;
  152. if (speed == XFER_UDMA_6)
  153. speed1 = 0x47;
  154. if (speed < XFER_UDMA_0) {
  155. u8 ultra_enable = (unit) ? 0x7f : 0xf7;
  156. /*
  157. * clear "ultra enable" bit
  158. */
  159. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  160. tmpbyte &= ultra_enable;
  161. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  162. /*
  163. * FIXME: Oh, my... DMA timings are never set.
  164. */
  165. } else {
  166. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  167. tmpbyte &= (0x0f << ((1-unit) << 2));
  168. /*
  169. * enable ultra dma and set timing
  170. */
  171. tmpbyte |= ((0x08 | ((4-speed1)&0x07)) << (unit << 2));
  172. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  173. if (speed >= XFER_UDMA_3) {
  174. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  175. tmpbyte |= 1;
  176. pci_write_config_byte(dev, 0x4b, tmpbyte);
  177. }
  178. }
  179. }
  180. /**
  181. * ali15x3_dma_setup - begin a DMA phase
  182. * @drive: target device
  183. *
  184. * Returns 1 if the DMA cannot be performed, zero on success.
  185. */
  186. static int ali15x3_dma_setup(ide_drive_t *drive)
  187. {
  188. if (m5229_revision < 0xC2 && drive->media != ide_disk) {
  189. if (rq_data_dir(drive->hwif->hwgroup->rq))
  190. return 1; /* try PIO instead of DMA */
  191. }
  192. return ide_dma_setup(drive);
  193. }
  194. /**
  195. * init_chipset_ali15x3 - Initialise an ALi IDE controller
  196. * @dev: PCI device
  197. * @name: Name of the controller
  198. *
  199. * This function initializes the ALI IDE controller and where
  200. * appropriate also sets up the 1533 southbridge.
  201. */
  202. static unsigned int __devinit init_chipset_ali15x3 (struct pci_dev *dev, const char *name)
  203. {
  204. unsigned long flags;
  205. u8 tmpbyte;
  206. struct pci_dev *north = pci_get_slot(dev->bus, PCI_DEVFN(0,0));
  207. m5229_revision = dev->revision;
  208. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  209. local_irq_save(flags);
  210. if (m5229_revision < 0xC2) {
  211. /*
  212. * revision 0x20 (1543-E, 1543-F)
  213. * revision 0xC0, 0xC1 (1543C-C, 1543C-D, 1543C-E)
  214. * clear CD-ROM DMA write bit, m5229, 0x4b, bit 7
  215. */
  216. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  217. /*
  218. * clear bit 7
  219. */
  220. pci_write_config_byte(dev, 0x4b, tmpbyte & 0x7F);
  221. /*
  222. * check m1533, 0x5e, bit 1~4 == 1001 => & 00011110 = 00010010
  223. */
  224. if (m5229_revision >= 0x20 && isa_dev) {
  225. pci_read_config_byte(isa_dev, 0x5e, &tmpbyte);
  226. chip_is_1543c_e = ((tmpbyte & 0x1e) == 0x12) ? 1: 0;
  227. }
  228. goto out;
  229. }
  230. /*
  231. * 1543C-B?, 1535, 1535D, 1553
  232. * Note 1: not all "motherboard" support this detection
  233. * Note 2: if no udma 66 device, the detection may "error".
  234. * but in this case, we will not set the device to
  235. * ultra 66, the detection result is not important
  236. */
  237. /*
  238. * enable "Cable Detection", m5229, 0x4b, bit3
  239. */
  240. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  241. pci_write_config_byte(dev, 0x4b, tmpbyte | 0x08);
  242. /*
  243. * We should only tune the 1533 enable if we are using an ALi
  244. * North bridge. We might have no north found on some zany
  245. * box without a device at 0:0.0. The ALi bridge will be at
  246. * 0:0.0 so if we didn't find one we know what is cooking.
  247. */
  248. if (north && north->vendor != PCI_VENDOR_ID_AL)
  249. goto out;
  250. if (m5229_revision < 0xC5 && isa_dev)
  251. {
  252. /*
  253. * set south-bridge's enable bit, m1533, 0x79
  254. */
  255. pci_read_config_byte(isa_dev, 0x79, &tmpbyte);
  256. if (m5229_revision == 0xC2) {
  257. /*
  258. * 1543C-B0 (m1533, 0x79, bit 2)
  259. */
  260. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x04);
  261. } else if (m5229_revision >= 0xC3) {
  262. /*
  263. * 1553/1535 (m1533, 0x79, bit 1)
  264. */
  265. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x02);
  266. }
  267. }
  268. out:
  269. /*
  270. * CD_ROM DMA on (m5229, 0x53, bit0)
  271. * Enable this bit even if we want to use PIO.
  272. * PIO FIFO off (m5229, 0x53, bit1)
  273. * The hardware will use 0x54h and 0x55h to control PIO FIFO.
  274. * (Not on later devices it seems)
  275. *
  276. * 0x53 changes meaning on later revs - we must no touch
  277. * bit 1 on them. Need to check if 0x20 is the right break.
  278. */
  279. if (m5229_revision >= 0x20) {
  280. pci_read_config_byte(dev, 0x53, &tmpbyte);
  281. if (m5229_revision <= 0x20)
  282. tmpbyte = (tmpbyte & (~0x02)) | 0x01;
  283. else if (m5229_revision == 0xc7 || m5229_revision == 0xc8)
  284. tmpbyte |= 0x03;
  285. else
  286. tmpbyte |= 0x01;
  287. pci_write_config_byte(dev, 0x53, tmpbyte);
  288. }
  289. pci_dev_put(north);
  290. pci_dev_put(isa_dev);
  291. local_irq_restore(flags);
  292. return 0;
  293. }
  294. /*
  295. * Cable special cases
  296. */
  297. static const struct dmi_system_id cable_dmi_table[] = {
  298. {
  299. .ident = "HP Pavilion N5430",
  300. .matches = {
  301. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  302. DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
  303. },
  304. },
  305. {
  306. .ident = "Toshiba Satellite S1800-814",
  307. .matches = {
  308. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  309. DMI_MATCH(DMI_PRODUCT_NAME, "S1800-814"),
  310. },
  311. },
  312. { }
  313. };
  314. static int ali_cable_override(struct pci_dev *pdev)
  315. {
  316. /* Fujitsu P2000 */
  317. if (pdev->subsystem_vendor == 0x10CF &&
  318. pdev->subsystem_device == 0x10AF)
  319. return 1;
  320. /* Mitac 8317 (Winbook-A) and relatives */
  321. if (pdev->subsystem_vendor == 0x1071 &&
  322. pdev->subsystem_device == 0x8317)
  323. return 1;
  324. /* Systems by DMI */
  325. if (dmi_check_system(cable_dmi_table))
  326. return 1;
  327. return 0;
  328. }
  329. /**
  330. * ali_cable_detect - cable detection
  331. * @hwif: IDE interface
  332. *
  333. * This checks if the controller and the cable are capable
  334. * of UDMA66 transfers. It doesn't check the drives.
  335. * But see note 2 below!
  336. *
  337. * FIXME: frobs bits that are not defined on newer ALi devicea
  338. */
  339. static u8 __devinit ali_cable_detect(ide_hwif_t *hwif)
  340. {
  341. struct pci_dev *dev = to_pci_dev(hwif->dev);
  342. unsigned long flags;
  343. u8 cbl = ATA_CBL_PATA40, tmpbyte;
  344. local_irq_save(flags);
  345. if (m5229_revision >= 0xC2) {
  346. /*
  347. * m5229 80-pin cable detection (from Host View)
  348. *
  349. * 0x4a bit0 is 0 => primary channel has 80-pin
  350. * 0x4a bit1 is 0 => secondary channel has 80-pin
  351. *
  352. * Certain laptops use short but suitable cables
  353. * and don't implement the detect logic.
  354. */
  355. if (ali_cable_override(dev))
  356. cbl = ATA_CBL_PATA40_SHORT;
  357. else {
  358. pci_read_config_byte(dev, 0x4a, &tmpbyte);
  359. if ((tmpbyte & (1 << hwif->channel)) == 0)
  360. cbl = ATA_CBL_PATA80;
  361. }
  362. }
  363. local_irq_restore(flags);
  364. return cbl;
  365. }
  366. #ifndef CONFIG_SPARC64
  367. /**
  368. * init_hwif_ali15x3 - Initialize the ALI IDE x86 stuff
  369. * @hwif: interface to configure
  370. *
  371. * Obtain the IRQ tables for an ALi based IDE solution on the PC
  372. * class platforms. This part of the code isn't applicable to the
  373. * Sparc systems
  374. */
  375. static void __devinit init_hwif_ali15x3 (ide_hwif_t *hwif)
  376. {
  377. struct pci_dev *dev = to_pci_dev(hwif->dev);
  378. u8 ideic, inmir;
  379. s8 irq_routing_table[] = { -1, 9, 3, 10, 4, 5, 7, 6,
  380. 1, 11, 0, 12, 0, 14, 0, 15 };
  381. int irq = -1;
  382. if (dev->device == PCI_DEVICE_ID_AL_M5229)
  383. hwif->irq = hwif->channel ? 15 : 14;
  384. if (isa_dev) {
  385. /*
  386. * read IDE interface control
  387. */
  388. pci_read_config_byte(isa_dev, 0x58, &ideic);
  389. /* bit0, bit1 */
  390. ideic = ideic & 0x03;
  391. /* get IRQ for IDE Controller */
  392. if ((hwif->channel && ideic == 0x03) ||
  393. (!hwif->channel && !ideic)) {
  394. /*
  395. * get SIRQ1 routing table
  396. */
  397. pci_read_config_byte(isa_dev, 0x44, &inmir);
  398. inmir = inmir & 0x0f;
  399. irq = irq_routing_table[inmir];
  400. } else if (hwif->channel && !(ideic & 0x01)) {
  401. /*
  402. * get SIRQ2 routing table
  403. */
  404. pci_read_config_byte(isa_dev, 0x75, &inmir);
  405. inmir = inmir & 0x0f;
  406. irq = irq_routing_table[inmir];
  407. }
  408. if(irq >= 0)
  409. hwif->irq = irq;
  410. }
  411. }
  412. #endif
  413. /**
  414. * init_dma_ali15x3 - set up DMA on ALi15x3
  415. * @hwif: IDE interface
  416. * @d: IDE port info
  417. *
  418. * Set up the DMA functionality on the ALi 15x3.
  419. */
  420. static int __devinit init_dma_ali15x3(ide_hwif_t *hwif,
  421. const struct ide_port_info *d)
  422. {
  423. struct pci_dev *dev = to_pci_dev(hwif->dev);
  424. unsigned long base = ide_pci_dma_base(hwif, d);
  425. if (base == 0 || ide_pci_set_master(dev, d->name) < 0)
  426. return -1;
  427. if (!hwif->channel)
  428. outb(inb(base + 2) & 0x60, base + 2);
  429. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  430. hwif->name, base, base + 7);
  431. if (ide_allocate_dma_engine(hwif))
  432. return -1;
  433. ide_setup_dma(hwif, base);
  434. return 0;
  435. }
  436. static const struct ide_port_ops ali_port_ops = {
  437. .set_pio_mode = ali_set_pio_mode,
  438. .set_dma_mode = ali_set_dma_mode,
  439. .udma_filter = ali_udma_filter,
  440. .cable_detect = ali_cable_detect,
  441. };
  442. static const struct ide_dma_ops ali_dma_ops = {
  443. .dma_host_set = ide_dma_host_set,
  444. .dma_setup = ali15x3_dma_setup,
  445. .dma_exec_cmd = ide_dma_exec_cmd,
  446. .dma_start = ide_dma_start,
  447. .dma_end = __ide_dma_end,
  448. .dma_test_irq = ide_dma_test_irq,
  449. .dma_lost_irq = ide_dma_lost_irq,
  450. .dma_timeout = ide_dma_timeout,
  451. };
  452. static const struct ide_port_info ali15x3_chipset __devinitdata = {
  453. .name = "ALI15X3",
  454. .init_chipset = init_chipset_ali15x3,
  455. #ifndef CONFIG_SPARC64
  456. .init_hwif = init_hwif_ali15x3,
  457. #endif
  458. .init_dma = init_dma_ali15x3,
  459. .port_ops = &ali_port_ops,
  460. .pio_mask = ATA_PIO5,
  461. .swdma_mask = ATA_SWDMA2,
  462. .mwdma_mask = ATA_MWDMA2,
  463. };
  464. /**
  465. * alim15x3_init_one - set up an ALi15x3 IDE controller
  466. * @dev: PCI device to set up
  467. *
  468. * Perform the actual set up for an ALi15x3 that has been found by the
  469. * hot plug layer.
  470. */
  471. static int __devinit alim15x3_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  472. {
  473. static struct pci_device_id ati_rs100[] = {
  474. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RS100) },
  475. { },
  476. };
  477. struct ide_port_info d = ali15x3_chipset;
  478. u8 rev = dev->revision, idx = id->driver_data;
  479. if (pci_dev_present(ati_rs100))
  480. printk(KERN_WARNING "alim15x3: ATI Radeon IGP Northbridge is not yet fully tested.\n");
  481. /* don't use LBA48 DMA on ALi devices before rev 0xC5 */
  482. if (rev <= 0xC4)
  483. d.host_flags |= IDE_HFLAG_NO_LBA48_DMA;
  484. if (rev >= 0x20) {
  485. if (rev == 0x20)
  486. d.host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
  487. if (rev < 0xC2)
  488. d.udma_mask = ATA_UDMA2;
  489. else if (rev == 0xC2 || rev == 0xC3)
  490. d.udma_mask = ATA_UDMA4;
  491. else if (rev == 0xC4)
  492. d.udma_mask = ATA_UDMA5;
  493. else
  494. d.udma_mask = ATA_UDMA6;
  495. d.dma_ops = &ali_dma_ops;
  496. } else {
  497. d.host_flags |= IDE_HFLAG_NO_DMA;
  498. d.mwdma_mask = d.swdma_mask = 0;
  499. }
  500. if (idx == 0)
  501. d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
  502. return ide_setup_pci_device(dev, &d);
  503. }
  504. static const struct pci_device_id alim15x3_pci_tbl[] = {
  505. { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5229), 0 },
  506. { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5228), 1 },
  507. { 0, },
  508. };
  509. MODULE_DEVICE_TABLE(pci, alim15x3_pci_tbl);
  510. static struct pci_driver driver = {
  511. .name = "ALI15x3_IDE",
  512. .id_table = alim15x3_pci_tbl,
  513. .probe = alim15x3_init_one,
  514. };
  515. static int __init ali15x3_ide_init(void)
  516. {
  517. return ide_pci_register_driver(&driver);
  518. }
  519. module_init(ali15x3_ide_init);
  520. MODULE_AUTHOR("Michael Aubry, Andrzej Krzysztofowicz, CJ, Andre Hedrick, Alan Cox");
  521. MODULE_DESCRIPTION("PCI driver module for ALi 15x3 IDE");
  522. MODULE_LICENSE("GPL");