alim15x3.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * linux/drivers/ide/pci/alim15x3.c Version 0.17 2003/01/02
  3. *
  4. * Copyright (C) 1998-2000 Michel Aubry, Maintainer
  5. * Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
  6. * Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
  7. *
  8. * Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
  9. * May be copied or modified under the terms of the GNU General Public License
  10. * Copyright (C) 2002 Alan Cox <alan@redhat.com>
  11. * ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
  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/delay.h>
  34. #include <linux/hdreg.h>
  35. #include <linux/ide.h>
  36. #include <linux/init.h>
  37. #include <asm/io.h>
  38. #define DISPLAY_ALI_TIMINGS
  39. /*
  40. * ALi devices are not plug in. Otherwise these static values would
  41. * need to go. They ought to go away anyway
  42. */
  43. static u8 m5229_revision;
  44. static u8 chip_is_1543c_e;
  45. static struct pci_dev *isa_dev;
  46. #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
  47. #include <linux/stat.h>
  48. #include <linux/proc_fs.h>
  49. static u8 ali_proc = 0;
  50. static struct pci_dev *bmide_dev;
  51. static char *fifo[4] = {
  52. "FIFO Off",
  53. "FIFO On ",
  54. "DMA mode",
  55. "PIO mode" };
  56. static char *udmaT[8] = {
  57. "1.5T",
  58. " 2T",
  59. "2.5T",
  60. " 3T",
  61. "3.5T",
  62. " 4T",
  63. " 6T",
  64. " 8T"
  65. };
  66. static char *channel_status[8] = {
  67. "OK ",
  68. "busy ",
  69. "DRQ ",
  70. "DRQ busy ",
  71. "error ",
  72. "error busy ",
  73. "error DRQ ",
  74. "error DRQ busy"
  75. };
  76. /**
  77. * ali_get_info - generate proc file for ALi IDE
  78. * @buffer: buffer to fill
  79. * @addr: address of user start in buffer
  80. * @offset: offset into 'file'
  81. * @count: buffer count
  82. *
  83. * Walks the Ali devices and outputs summary data on the tuning and
  84. * anything else that will help with debugging
  85. */
  86. static int ali_get_info (char *buffer, char **addr, off_t offset, int count)
  87. {
  88. unsigned long bibma;
  89. u8 reg53h, reg5xh, reg5yh, reg5xh1, reg5yh1, c0, c1, rev, tmp;
  90. char *q, *p = buffer;
  91. /* fetch rev. */
  92. pci_read_config_byte(bmide_dev, 0x08, &rev);
  93. if (rev >= 0xc1) /* M1543C or newer */
  94. udmaT[7] = " ???";
  95. else
  96. fifo[3] = " ??? ";
  97. /* first fetch bibma: */
  98. bibma = pci_resource_start(bmide_dev, 4);
  99. /*
  100. * at that point bibma+0x2 et bibma+0xa are byte
  101. * registers to investigate:
  102. */
  103. c0 = inb(bibma + 0x02);
  104. c1 = inb(bibma + 0x0a);
  105. p += sprintf(p,
  106. "\n Ali M15x3 Chipset.\n");
  107. p += sprintf(p,
  108. " ------------------\n");
  109. pci_read_config_byte(bmide_dev, 0x78, &reg53h);
  110. p += sprintf(p, "PCI Clock: %d.\n", reg53h);
  111. pci_read_config_byte(bmide_dev, 0x53, &reg53h);
  112. p += sprintf(p,
  113. "CD_ROM FIFO:%s, CD_ROM DMA:%s\n",
  114. (reg53h & 0x02) ? "Yes" : "No ",
  115. (reg53h & 0x01) ? "Yes" : "No " );
  116. pci_read_config_byte(bmide_dev, 0x74, &reg53h);
  117. p += sprintf(p,
  118. "FIFO Status: contains %d Words, runs%s%s\n\n",
  119. (reg53h & 0x3f),
  120. (reg53h & 0x40) ? " OVERWR" : "",
  121. (reg53h & 0x80) ? " OVERRD." : "." );
  122. p += sprintf(p,
  123. "-------------------primary channel"
  124. "-------------------secondary channel"
  125. "---------\n\n");
  126. pci_read_config_byte(bmide_dev, 0x09, &reg53h);
  127. p += sprintf(p,
  128. "channel status: %s"
  129. " %s\n",
  130. (reg53h & 0x20) ? "On " : "Off",
  131. (reg53h & 0x10) ? "On " : "Off" );
  132. p += sprintf(p,
  133. "both channels togth: %s"
  134. " %s\n",
  135. (c0&0x80) ? "No " : "Yes",
  136. (c1&0x80) ? "No " : "Yes" );
  137. pci_read_config_byte(bmide_dev, 0x76, &reg53h);
  138. p += sprintf(p,
  139. "Channel state: %s %s\n",
  140. channel_status[reg53h & 0x07],
  141. channel_status[(reg53h & 0x70) >> 4] );
  142. pci_read_config_byte(bmide_dev, 0x58, &reg5xh);
  143. pci_read_config_byte(bmide_dev, 0x5c, &reg5yh);
  144. p += sprintf(p,
  145. "Add. Setup Timing: %dT"
  146. " %dT\n",
  147. (reg5xh & 0x07) ? (reg5xh & 0x07) : 8,
  148. (reg5yh & 0x07) ? (reg5yh & 0x07) : 8 );
  149. pci_read_config_byte(bmide_dev, 0x59, &reg5xh);
  150. pci_read_config_byte(bmide_dev, 0x5d, &reg5yh);
  151. p += sprintf(p,
  152. "Command Act. Count: %dT"
  153. " %dT\n"
  154. "Command Rec. Count: %dT"
  155. " %dT\n\n",
  156. (reg5xh & 0x70) ? ((reg5xh & 0x70) >> 4) : 8,
  157. (reg5yh & 0x70) ? ((reg5yh & 0x70) >> 4) : 8,
  158. (reg5xh & 0x0f) ? (reg5xh & 0x0f) : 16,
  159. (reg5yh & 0x0f) ? (reg5yh & 0x0f) : 16 );
  160. p += sprintf(p,
  161. "----------------drive0-----------drive1"
  162. "------------drive0-----------drive1------\n\n");
  163. p += sprintf(p,
  164. "DMA enabled: %s %s"
  165. " %s %s\n",
  166. (c0&0x20) ? "Yes" : "No ",
  167. (c0&0x40) ? "Yes" : "No ",
  168. (c1&0x20) ? "Yes" : "No ",
  169. (c1&0x40) ? "Yes" : "No " );
  170. pci_read_config_byte(bmide_dev, 0x54, &reg5xh);
  171. pci_read_config_byte(bmide_dev, 0x55, &reg5yh);
  172. q = "FIFO threshold: %2d Words %2d Words"
  173. " %2d Words %2d Words\n";
  174. if (rev < 0xc1) {
  175. if ((rev == 0x20) &&
  176. (pci_read_config_byte(bmide_dev, 0x4f, &tmp), (tmp &= 0x20))) {
  177. p += sprintf(p, q, 8, 8, 8, 8);
  178. } else {
  179. p += sprintf(p, q,
  180. (reg5xh & 0x03) + 12,
  181. ((reg5xh & 0x30)>>4) + 12,
  182. (reg5yh & 0x03) + 12,
  183. ((reg5yh & 0x30)>>4) + 12 );
  184. }
  185. } else {
  186. int t1 = (tmp = (reg5xh & 0x03)) ? (tmp << 3) : 4;
  187. int t2 = (tmp = ((reg5xh & 0x30)>>4)) ? (tmp << 3) : 4;
  188. int t3 = (tmp = (reg5yh & 0x03)) ? (tmp << 3) : 4;
  189. int t4 = (tmp = ((reg5yh & 0x30)>>4)) ? (tmp << 3) : 4;
  190. p += sprintf(p, q, t1, t2, t3, t4);
  191. }
  192. #if 0
  193. p += sprintf(p,
  194. "FIFO threshold: %2d Words %2d Words"
  195. " %2d Words %2d Words\n",
  196. (reg5xh & 0x03) + 12,
  197. ((reg5xh & 0x30)>>4) + 12,
  198. (reg5yh & 0x03) + 12,
  199. ((reg5yh & 0x30)>>4) + 12 );
  200. #endif
  201. p += sprintf(p,
  202. "FIFO mode: %s %s %s %s\n",
  203. fifo[((reg5xh & 0x0c) >> 2)],
  204. fifo[((reg5xh & 0xc0) >> 6)],
  205. fifo[((reg5yh & 0x0c) >> 2)],
  206. fifo[((reg5yh & 0xc0) >> 6)] );
  207. pci_read_config_byte(bmide_dev, 0x5a, &reg5xh);
  208. pci_read_config_byte(bmide_dev, 0x5b, &reg5xh1);
  209. pci_read_config_byte(bmide_dev, 0x5e, &reg5yh);
  210. pci_read_config_byte(bmide_dev, 0x5f, &reg5yh1);
  211. p += sprintf(p,/*
  212. "------------------drive0-----------drive1"
  213. "------------drive0-----------drive1------\n")*/
  214. "Dt RW act. Cnt %2dT %2dT"
  215. " %2dT %2dT\n"
  216. "Dt RW rec. Cnt %2dT %2dT"
  217. " %2dT %2dT\n\n",
  218. (reg5xh & 0x70) ? ((reg5xh & 0x70) >> 4) : 8,
  219. (reg5xh1 & 0x70) ? ((reg5xh1 & 0x70) >> 4) : 8,
  220. (reg5yh & 0x70) ? ((reg5yh & 0x70) >> 4) : 8,
  221. (reg5yh1 & 0x70) ? ((reg5yh1 & 0x70) >> 4) : 8,
  222. (reg5xh & 0x0f) ? (reg5xh & 0x0f) : 16,
  223. (reg5xh1 & 0x0f) ? (reg5xh1 & 0x0f) : 16,
  224. (reg5yh & 0x0f) ? (reg5yh & 0x0f) : 16,
  225. (reg5yh1 & 0x0f) ? (reg5yh1 & 0x0f) : 16 );
  226. p += sprintf(p,
  227. "-----------------------------------UDMA Timings"
  228. "--------------------------------\n\n");
  229. pci_read_config_byte(bmide_dev, 0x56, &reg5xh);
  230. pci_read_config_byte(bmide_dev, 0x57, &reg5yh);
  231. p += sprintf(p,
  232. "UDMA: %s %s"
  233. " %s %s\n"
  234. "UDMA timings: %s %s"
  235. " %s %s\n\n",
  236. (reg5xh & 0x08) ? "OK" : "No",
  237. (reg5xh & 0x80) ? "OK" : "No",
  238. (reg5yh & 0x08) ? "OK" : "No",
  239. (reg5yh & 0x80) ? "OK" : "No",
  240. udmaT[(reg5xh & 0x07)],
  241. udmaT[(reg5xh & 0x70) >> 4],
  242. udmaT[reg5yh & 0x07],
  243. udmaT[(reg5yh & 0x70) >> 4] );
  244. return p-buffer; /* => must be less than 4k! */
  245. }
  246. #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
  247. /**
  248. * ali15x3_tune_drive - set up a drive
  249. * @drive: drive to tune
  250. * @pio: unused
  251. *
  252. * Select the best PIO timing for the drive in question. Then
  253. * program the controller for this drive set up
  254. */
  255. static void ali15x3_tune_drive (ide_drive_t *drive, u8 pio)
  256. {
  257. ide_pio_data_t d;
  258. ide_hwif_t *hwif = HWIF(drive);
  259. struct pci_dev *dev = hwif->pci_dev;
  260. int s_time, a_time, c_time;
  261. u8 s_clc, a_clc, r_clc;
  262. unsigned long flags;
  263. int bus_speed = system_bus_clock();
  264. int port = hwif->channel ? 0x5c : 0x58;
  265. int portFIFO = hwif->channel ? 0x55 : 0x54;
  266. u8 cd_dma_fifo = 0;
  267. int unit = drive->select.b.unit & 1;
  268. pio = ide_get_best_pio_mode(drive, pio, 5, &d);
  269. s_time = ide_pio_timings[pio].setup_time;
  270. a_time = ide_pio_timings[pio].active_time;
  271. if ((s_clc = (s_time * bus_speed + 999) / 1000) >= 8)
  272. s_clc = 0;
  273. if ((a_clc = (a_time * bus_speed + 999) / 1000) >= 8)
  274. a_clc = 0;
  275. c_time = ide_pio_timings[pio].cycle_time;
  276. #if 0
  277. if ((r_clc = ((c_time - s_time - a_time) * bus_speed + 999) / 1000) >= 16)
  278. r_clc = 0;
  279. #endif
  280. if (!(r_clc = (c_time * bus_speed + 999) / 1000 - a_clc - s_clc)) {
  281. r_clc = 1;
  282. } else {
  283. if (r_clc >= 16)
  284. r_clc = 0;
  285. }
  286. local_irq_save(flags);
  287. /*
  288. * PIO mode => ATA FIFO on, ATAPI FIFO off
  289. */
  290. pci_read_config_byte(dev, portFIFO, &cd_dma_fifo);
  291. if (drive->media==ide_disk) {
  292. if (unit) {
  293. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0x0F) | 0x50);
  294. } else {
  295. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0xF0) | 0x05);
  296. }
  297. } else {
  298. if (unit) {
  299. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0x0F);
  300. } else {
  301. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0xF0);
  302. }
  303. }
  304. pci_write_config_byte(dev, port, s_clc);
  305. pci_write_config_byte(dev, port+drive->select.b.unit+2, (a_clc << 4) | r_clc);
  306. local_irq_restore(flags);
  307. /*
  308. * setup active rec
  309. * { 70, 165, 365 }, PIO Mode 0
  310. * { 50, 125, 208 }, PIO Mode 1
  311. * { 30, 100, 110 }, PIO Mode 2
  312. * { 30, 80, 70 }, PIO Mode 3 with IORDY
  313. * { 25, 70, 25 }, PIO Mode 4 with IORDY ns
  314. * { 20, 50, 30 } PIO Mode 5 with IORDY (nonstandard)
  315. */
  316. }
  317. /**
  318. * ali15x3_can_ultra - check for ultra DMA support
  319. * @drive: drive to do the check
  320. *
  321. * Check the drive and controller revisions. Return 0 if UDMA is
  322. * not available, or 1 if UDMA can be used. The actual rules for
  323. * the ALi are
  324. * No UDMA on revisions <= 0x20
  325. * Disk only for revisions < 0xC2
  326. * Not WDC drives for revisions < 0xC2
  327. *
  328. * FIXME: WDC ifdef needs to die
  329. */
  330. static u8 ali15x3_can_ultra (ide_drive_t *drive)
  331. {
  332. #ifndef CONFIG_WDC_ALI15X3
  333. struct hd_driveid *id = drive->id;
  334. #endif /* CONFIG_WDC_ALI15X3 */
  335. if (m5229_revision <= 0x20) {
  336. return 0;
  337. } else if ((m5229_revision < 0xC2) &&
  338. #ifndef CONFIG_WDC_ALI15X3
  339. ((chip_is_1543c_e && strstr(id->model, "WDC ")) ||
  340. (drive->media!=ide_disk))) {
  341. #else /* CONFIG_WDC_ALI15X3 */
  342. (drive->media!=ide_disk)) {
  343. #endif /* CONFIG_WDC_ALI15X3 */
  344. return 0;
  345. } else {
  346. return 1;
  347. }
  348. }
  349. /**
  350. * ali15x3_ratemask - generate DMA mode list
  351. * @drive: drive to compute against
  352. *
  353. * Generate a list of the available DMA modes for the drive.
  354. * FIXME: this function contains lots of bogus masking we can dump
  355. *
  356. * Return the highest available mode (UDMA33, UDMA66, UDMA100,..)
  357. */
  358. static u8 ali15x3_ratemask (ide_drive_t *drive)
  359. {
  360. u8 mode = 0, can_ultra = ali15x3_can_ultra(drive);
  361. if (m5229_revision > 0xC4 && can_ultra) {
  362. mode = 4;
  363. } else if (m5229_revision == 0xC4 && can_ultra) {
  364. mode = 3;
  365. } else if (m5229_revision >= 0xC2 && can_ultra) {
  366. mode = 2;
  367. } else if (can_ultra) {
  368. return 1;
  369. } else {
  370. return 0;
  371. }
  372. /*
  373. * If the drive sees no suitable cable then UDMA 33
  374. * is the highest permitted mode
  375. */
  376. if (!eighty_ninty_three(drive))
  377. mode = min(mode, (u8)1);
  378. return mode;
  379. }
  380. /**
  381. * ali15x3_tune_chipset - set up chiset for new speed
  382. * @drive: drive to configure for
  383. * @xferspeed: desired speed
  384. *
  385. * Configure the hardware for the desired IDE transfer mode.
  386. * We also do the needed drive configuration through helpers
  387. */
  388. static int ali15x3_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  389. {
  390. ide_hwif_t *hwif = HWIF(drive);
  391. struct pci_dev *dev = hwif->pci_dev;
  392. u8 speed = ide_rate_filter(ali15x3_ratemask(drive), xferspeed);
  393. u8 speed1 = speed;
  394. u8 unit = (drive->select.b.unit & 0x01);
  395. u8 tmpbyte = 0x00;
  396. int m5229_udma = (hwif->channel) ? 0x57 : 0x56;
  397. if (speed == XFER_UDMA_6)
  398. speed1 = 0x47;
  399. if (speed < XFER_UDMA_0) {
  400. u8 ultra_enable = (unit) ? 0x7f : 0xf7;
  401. /*
  402. * clear "ultra enable" bit
  403. */
  404. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  405. tmpbyte &= ultra_enable;
  406. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  407. if (speed < XFER_SW_DMA_0)
  408. ali15x3_tune_drive(drive, speed);
  409. } else {
  410. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  411. tmpbyte &= (0x0f << ((1-unit) << 2));
  412. /*
  413. * enable ultra dma and set timing
  414. */
  415. tmpbyte |= ((0x08 | ((4-speed1)&0x07)) << (unit << 2));
  416. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  417. if (speed >= XFER_UDMA_3) {
  418. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  419. tmpbyte |= 1;
  420. pci_write_config_byte(dev, 0x4b, tmpbyte);
  421. }
  422. }
  423. return (ide_config_drive_speed(drive, speed));
  424. }
  425. /**
  426. * config_chipset_for_dma - set up DMA mode
  427. * @drive: drive to configure for
  428. *
  429. * Place a drive into DMA mode and tune the chipset for
  430. * the selected speed.
  431. *
  432. * Returns true if DMA mode can be used
  433. */
  434. static int config_chipset_for_dma (ide_drive_t *drive)
  435. {
  436. u8 speed = ide_dma_speed(drive, ali15x3_ratemask(drive));
  437. if (!(speed))
  438. return 0;
  439. (void) ali15x3_tune_chipset(drive, speed);
  440. return ide_dma_enable(drive);
  441. }
  442. /**
  443. * ali15x3_config_drive_for_dma - configure for DMA
  444. * @drive: drive to configure
  445. *
  446. * Configure a drive for DMA operation. If DMA is not possible we
  447. * drop the drive into PIO mode instead.
  448. *
  449. * FIXME: exactly what are we trying to return here
  450. */
  451. static int ali15x3_config_drive_for_dma(ide_drive_t *drive)
  452. {
  453. ide_hwif_t *hwif = HWIF(drive);
  454. struct hd_driveid *id = drive->id;
  455. if ((m5229_revision<=0x20) && (drive->media!=ide_disk))
  456. return hwif->ide_dma_off_quietly(drive);
  457. drive->init_speed = 0;
  458. if ((id != NULL) && ((id->capability & 1) != 0) && drive->autodma) {
  459. /* Consult the list of known "bad" drives */
  460. if (__ide_dma_bad_drive(drive))
  461. goto ata_pio;
  462. if ((id->field_valid & 4) && (m5229_revision >= 0xC2)) {
  463. if (id->dma_ultra & hwif->ultra_mask) {
  464. /* Force if Capable UltraDMA */
  465. int dma = config_chipset_for_dma(drive);
  466. if ((id->field_valid & 2) && !dma)
  467. goto try_dma_modes;
  468. }
  469. } else if (id->field_valid & 2) {
  470. try_dma_modes:
  471. if ((id->dma_mword & hwif->mwdma_mask) ||
  472. (id->dma_1word & hwif->swdma_mask)) {
  473. /* Force if Capable regular DMA modes */
  474. if (!config_chipset_for_dma(drive))
  475. goto no_dma_set;
  476. }
  477. } else if (__ide_dma_good_drive(drive) &&
  478. (id->eide_dma_time < 150)) {
  479. /* Consult the list of known "good" drives */
  480. if (!config_chipset_for_dma(drive))
  481. goto no_dma_set;
  482. } else {
  483. goto ata_pio;
  484. }
  485. } else {
  486. ata_pio:
  487. hwif->tuneproc(drive, 255);
  488. no_dma_set:
  489. return hwif->ide_dma_off_quietly(drive);
  490. }
  491. return hwif->ide_dma_on(drive);
  492. }
  493. /**
  494. * ali15x3_dma_setup - begin a DMA phase
  495. * @drive: target device
  496. *
  497. * Returns 1 if the DMA cannot be performed, zero on success.
  498. */
  499. static int ali15x3_dma_setup(ide_drive_t *drive)
  500. {
  501. if (m5229_revision < 0xC2 && drive->media != ide_disk) {
  502. if (rq_data_dir(drive->hwif->hwgroup->rq))
  503. return 1; /* try PIO instead of DMA */
  504. }
  505. return ide_dma_setup(drive);
  506. }
  507. /**
  508. * init_chipset_ali15x3 - Initialise an ALi IDE controller
  509. * @dev: PCI device
  510. * @name: Name of the controller
  511. *
  512. * This function initializes the ALI IDE controller and where
  513. * appropriate also sets up the 1533 southbridge.
  514. */
  515. static unsigned int __devinit init_chipset_ali15x3 (struct pci_dev *dev, const char *name)
  516. {
  517. unsigned long flags;
  518. u8 tmpbyte;
  519. struct pci_dev *north = pci_find_slot(0, PCI_DEVFN(0,0));
  520. pci_read_config_byte(dev, PCI_REVISION_ID, &m5229_revision);
  521. isa_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  522. #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
  523. if (!ali_proc) {
  524. ali_proc = 1;
  525. bmide_dev = dev;
  526. ide_pci_create_host_proc("ali", ali_get_info);
  527. }
  528. #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
  529. local_irq_save(flags);
  530. if (m5229_revision < 0xC2) {
  531. /*
  532. * revision 0x20 (1543-E, 1543-F)
  533. * revision 0xC0, 0xC1 (1543C-C, 1543C-D, 1543C-E)
  534. * clear CD-ROM DMA write bit, m5229, 0x4b, bit 7
  535. */
  536. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  537. /*
  538. * clear bit 7
  539. */
  540. pci_write_config_byte(dev, 0x4b, tmpbyte & 0x7F);
  541. local_irq_restore(flags);
  542. return 0;
  543. }
  544. /*
  545. * 1543C-B?, 1535, 1535D, 1553
  546. * Note 1: not all "motherboard" support this detection
  547. * Note 2: if no udma 66 device, the detection may "error".
  548. * but in this case, we will not set the device to
  549. * ultra 66, the detection result is not important
  550. */
  551. /*
  552. * enable "Cable Detection", m5229, 0x4b, bit3
  553. */
  554. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  555. pci_write_config_byte(dev, 0x4b, tmpbyte | 0x08);
  556. /*
  557. * We should only tune the 1533 enable if we are using an ALi
  558. * North bridge. We might have no north found on some zany
  559. * box without a device at 0:0.0. The ALi bridge will be at
  560. * 0:0.0 so if we didn't find one we know what is cooking.
  561. */
  562. if (north && north->vendor != PCI_VENDOR_ID_AL) {
  563. local_irq_restore(flags);
  564. return 0;
  565. }
  566. if (m5229_revision < 0xC5 && isa_dev)
  567. {
  568. /*
  569. * set south-bridge's enable bit, m1533, 0x79
  570. */
  571. pci_read_config_byte(isa_dev, 0x79, &tmpbyte);
  572. if (m5229_revision == 0xC2) {
  573. /*
  574. * 1543C-B0 (m1533, 0x79, bit 2)
  575. */
  576. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x04);
  577. } else if (m5229_revision >= 0xC3) {
  578. /*
  579. * 1553/1535 (m1533, 0x79, bit 1)
  580. */
  581. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x02);
  582. }
  583. }
  584. local_irq_restore(flags);
  585. return 0;
  586. }
  587. /**
  588. * ata66_ali15x3 - check for UDMA 66 support
  589. * @hwif: IDE interface
  590. *
  591. * This checks if the controller and the cable are capable
  592. * of UDMA66 transfers. It doesn't check the drives.
  593. * But see note 2 below!
  594. *
  595. * FIXME: frobs bits that are not defined on newer ALi devicea
  596. */
  597. static unsigned int __devinit ata66_ali15x3 (ide_hwif_t *hwif)
  598. {
  599. struct pci_dev *dev = hwif->pci_dev;
  600. unsigned int ata66 = 0;
  601. u8 cable_80_pin[2] = { 0, 0 };
  602. unsigned long flags;
  603. u8 tmpbyte;
  604. local_irq_save(flags);
  605. if (m5229_revision >= 0xC2) {
  606. /*
  607. * Ultra66 cable detection (from Host View)
  608. * m5229, 0x4a, bit0: primary, bit1: secondary 80 pin
  609. */
  610. pci_read_config_byte(dev, 0x4a, &tmpbyte);
  611. /*
  612. * 0x4a, bit0 is 0 => primary channel
  613. * has 80-pin (from host view)
  614. */
  615. if (!(tmpbyte & 0x01)) cable_80_pin[0] = 1;
  616. /*
  617. * 0x4a, bit1 is 0 => secondary channel
  618. * has 80-pin (from host view)
  619. */
  620. if (!(tmpbyte & 0x02)) cable_80_pin[1] = 1;
  621. /*
  622. * Allow ata66 if cable of current channel has 80 pins
  623. */
  624. ata66 = (hwif->channel)?cable_80_pin[1]:cable_80_pin[0];
  625. } else {
  626. /*
  627. * check m1533, 0x5e, bit 1~4 == 1001 => & 00011110 = 00010010
  628. */
  629. pci_read_config_byte(isa_dev, 0x5e, &tmpbyte);
  630. chip_is_1543c_e = ((tmpbyte & 0x1e) == 0x12) ? 1: 0;
  631. }
  632. /*
  633. * CD_ROM DMA on (m5229, 0x53, bit0)
  634. * Enable this bit even if we want to use PIO
  635. * PIO FIFO off (m5229, 0x53, bit1)
  636. * The hardware will use 0x54h and 0x55h to control PIO FIFO
  637. * (Not on later devices it seems)
  638. *
  639. * 0x53 changes meaning on later revs - we must no touch
  640. * bit 1 on them. Need to check if 0x20 is the right break
  641. */
  642. pci_read_config_byte(dev, 0x53, &tmpbyte);
  643. if(m5229_revision <= 0x20)
  644. tmpbyte = (tmpbyte & (~0x02)) | 0x01;
  645. else if (m5229_revision == 0xc7 || m5229_revision == 0xc8)
  646. tmpbyte |= 0x03;
  647. else
  648. tmpbyte |= 0x01;
  649. pci_write_config_byte(dev, 0x53, tmpbyte);
  650. local_irq_restore(flags);
  651. return(ata66);
  652. }
  653. /**
  654. * init_hwif_common_ali15x3 - Set up ALI IDE hardware
  655. * @hwif: IDE interface
  656. *
  657. * Initialize the IDE structure side of the ALi 15x3 driver.
  658. */
  659. static void __devinit init_hwif_common_ali15x3 (ide_hwif_t *hwif)
  660. {
  661. hwif->autodma = 0;
  662. hwif->tuneproc = &ali15x3_tune_drive;
  663. hwif->speedproc = &ali15x3_tune_chipset;
  664. /* don't use LBA48 DMA on ALi devices before rev 0xC5 */
  665. hwif->no_lba48_dma = (m5229_revision <= 0xC4) ? 1 : 0;
  666. if (!hwif->dma_base) {
  667. hwif->drives[0].autotune = 1;
  668. hwif->drives[1].autotune = 1;
  669. return;
  670. }
  671. hwif->atapi_dma = 1;
  672. if (m5229_revision > 0x20)
  673. hwif->ultra_mask = 0x7f;
  674. hwif->mwdma_mask = 0x07;
  675. hwif->swdma_mask = 0x07;
  676. if (m5229_revision >= 0x20) {
  677. /*
  678. * M1543C or newer for DMAing
  679. */
  680. hwif->ide_dma_check = &ali15x3_config_drive_for_dma;
  681. hwif->dma_setup = &ali15x3_dma_setup;
  682. if (!noautodma)
  683. hwif->autodma = 1;
  684. if (!(hwif->udma_four))
  685. hwif->udma_four = ata66_ali15x3(hwif);
  686. }
  687. hwif->drives[0].autodma = hwif->autodma;
  688. hwif->drives[1].autodma = hwif->autodma;
  689. }
  690. /**
  691. * init_hwif_ali15x3 - Initialize the ALI IDE x86 stuff
  692. * @hwif: interface to configure
  693. *
  694. * Obtain the IRQ tables for an ALi based IDE solution on the PC
  695. * class platforms. This part of the code isn't applicable to the
  696. * Sparc systems
  697. */
  698. static void __devinit init_hwif_ali15x3 (ide_hwif_t *hwif)
  699. {
  700. u8 ideic, inmir;
  701. s8 irq_routing_table[] = { -1, 9, 3, 10, 4, 5, 7, 6,
  702. 1, 11, 0, 12, 0, 14, 0, 15 };
  703. int irq = -1;
  704. if (hwif->pci_dev->device == PCI_DEVICE_ID_AL_M5229)
  705. hwif->irq = hwif->channel ? 15 : 14;
  706. if (isa_dev) {
  707. /*
  708. * read IDE interface control
  709. */
  710. pci_read_config_byte(isa_dev, 0x58, &ideic);
  711. /* bit0, bit1 */
  712. ideic = ideic & 0x03;
  713. /* get IRQ for IDE Controller */
  714. if ((hwif->channel && ideic == 0x03) ||
  715. (!hwif->channel && !ideic)) {
  716. /*
  717. * get SIRQ1 routing table
  718. */
  719. pci_read_config_byte(isa_dev, 0x44, &inmir);
  720. inmir = inmir & 0x0f;
  721. irq = irq_routing_table[inmir];
  722. } else if (hwif->channel && !(ideic & 0x01)) {
  723. /*
  724. * get SIRQ2 routing table
  725. */
  726. pci_read_config_byte(isa_dev, 0x75, &inmir);
  727. inmir = inmir & 0x0f;
  728. irq = irq_routing_table[inmir];
  729. }
  730. if(irq >= 0)
  731. hwif->irq = irq;
  732. }
  733. init_hwif_common_ali15x3(hwif);
  734. }
  735. /**
  736. * init_dma_ali15x3 - set up DMA on ALi15x3
  737. * @hwif: IDE interface
  738. * @dmabase: DMA interface base PCI address
  739. *
  740. * Set up the DMA functionality on the ALi 15x3. For the ALi
  741. * controllers this is generic so we can let the generic code do
  742. * the actual work.
  743. */
  744. static void __devinit init_dma_ali15x3 (ide_hwif_t *hwif, unsigned long dmabase)
  745. {
  746. if (m5229_revision < 0x20)
  747. return;
  748. if (!(hwif->channel))
  749. hwif->OUTB(hwif->INB(dmabase+2) & 0x60, dmabase+2);
  750. ide_setup_dma(hwif, dmabase, 8);
  751. }
  752. static ide_pci_device_t ali15x3_chipset __devinitdata = {
  753. .name = "ALI15X3",
  754. .init_chipset = init_chipset_ali15x3,
  755. .init_hwif = init_hwif_ali15x3,
  756. .init_dma = init_dma_ali15x3,
  757. .channels = 2,
  758. .autodma = AUTODMA,
  759. .bootable = ON_BOARD,
  760. };
  761. /**
  762. * alim15x3_init_one - set up an ALi15x3 IDE controller
  763. * @dev: PCI device to set up
  764. *
  765. * Perform the actual set up for an ALi15x3 that has been found by the
  766. * hot plug layer.
  767. */
  768. static int __devinit alim15x3_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  769. {
  770. static struct pci_device_id ati_rs100[] = {
  771. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RS100) },
  772. { },
  773. };
  774. ide_pci_device_t *d = &ali15x3_chipset;
  775. if (pci_dev_present(ati_rs100))
  776. printk(KERN_WARNING "alim15x3: ATI Radeon IGP Northbridge is not yet fully tested.\n");
  777. #if defined(CONFIG_SPARC64)
  778. d->init_hwif = init_hwif_common_ali15x3;
  779. #endif /* CONFIG_SPARC64 */
  780. return ide_setup_pci_device(dev, d);
  781. }
  782. static struct pci_device_id alim15x3_pci_tbl[] = {
  783. { PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5229, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  784. { PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5228, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  785. { 0, },
  786. };
  787. MODULE_DEVICE_TABLE(pci, alim15x3_pci_tbl);
  788. static struct pci_driver driver = {
  789. .name = "ALI15x3_IDE",
  790. .id_table = alim15x3_pci_tbl,
  791. .probe = alim15x3_init_one,
  792. };
  793. static int ali15x3_ide_init(void)
  794. {
  795. return ide_pci_register_driver(&driver);
  796. }
  797. module_init(ali15x3_ide_init);
  798. MODULE_AUTHOR("Michael Aubry, Andrzej Krzysztofowicz, CJ, Andre Hedrick, Alan Cox");
  799. MODULE_DESCRIPTION("PCI driver module for ALi 15x3 IDE");
  800. MODULE_LICENSE("GPL");