scc_pata.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. * Support for IDE interfaces on Celleb platform
  3. *
  4. * (C) Copyright 2006 TOSHIBA CORPORATION
  5. *
  6. * This code is based on drivers/ide/pci/siimage.c:
  7. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  8. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <linux/hdreg.h>
  29. #include <linux/ide.h>
  30. #include <linux/init.h>
  31. #define PCI_DEVICE_ID_TOSHIBA_SCC_ATA 0x01b4
  32. #define SCC_PATA_NAME "scc IDE"
  33. #define TDVHSEL_MASTER 0x00000001
  34. #define TDVHSEL_SLAVE 0x00000004
  35. #define MODE_JCUSFEN 0x00000080
  36. #define CCKCTRL_ATARESET 0x00040000
  37. #define CCKCTRL_BUFCNT 0x00020000
  38. #define CCKCTRL_CRST 0x00010000
  39. #define CCKCTRL_OCLKEN 0x00000100
  40. #define CCKCTRL_ATACLKOEN 0x00000002
  41. #define CCKCTRL_LCLKEN 0x00000001
  42. #define QCHCD_IOS_SS 0x00000001
  43. #define QCHSD_STPDIAG 0x00020000
  44. #define INTMASK_MSK 0xD1000012
  45. #define INTSTS_SERROR 0x80000000
  46. #define INTSTS_PRERR 0x40000000
  47. #define INTSTS_RERR 0x10000000
  48. #define INTSTS_ICERR 0x01000000
  49. #define INTSTS_BMSINT 0x00000010
  50. #define INTSTS_BMHE 0x00000008
  51. #define INTSTS_IOIRQS 0x00000004
  52. #define INTSTS_INTRQ 0x00000002
  53. #define INTSTS_ACTEINT 0x00000001
  54. #define ECMODE_VALUE 0x01
  55. static struct scc_ports {
  56. unsigned long ctl, dma;
  57. ide_hwif_t *hwif; /* for removing port from system */
  58. } scc_ports[MAX_HWIFS];
  59. /* PIO transfer mode table */
  60. /* JCHST */
  61. static unsigned long JCHSTtbl[2][7] = {
  62. {0x0E, 0x05, 0x02, 0x03, 0x02, 0x00, 0x00}, /* 100MHz */
  63. {0x13, 0x07, 0x04, 0x04, 0x03, 0x00, 0x00} /* 133MHz */
  64. };
  65. /* JCHHT */
  66. static unsigned long JCHHTtbl[2][7] = {
  67. {0x0E, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00}, /* 100MHz */
  68. {0x13, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00} /* 133MHz */
  69. };
  70. /* JCHCT */
  71. static unsigned long JCHCTtbl[2][7] = {
  72. {0x1D, 0x1D, 0x1C, 0x0B, 0x06, 0x00, 0x00}, /* 100MHz */
  73. {0x27, 0x26, 0x26, 0x0E, 0x09, 0x00, 0x00} /* 133MHz */
  74. };
  75. /* DMA transfer mode table */
  76. /* JCHDCTM/JCHDCTS */
  77. static unsigned long JCHDCTxtbl[2][7] = {
  78. {0x0A, 0x06, 0x04, 0x03, 0x01, 0x00, 0x00}, /* 100MHz */
  79. {0x0E, 0x09, 0x06, 0x04, 0x02, 0x01, 0x00} /* 133MHz */
  80. };
  81. /* JCSTWTM/JCSTWTS */
  82. static unsigned long JCSTWTxtbl[2][7] = {
  83. {0x06, 0x04, 0x03, 0x02, 0x02, 0x02, 0x00}, /* 100MHz */
  84. {0x09, 0x06, 0x04, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  85. };
  86. /* JCTSS */
  87. static unsigned long JCTSStbl[2][7] = {
  88. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00}, /* 100MHz */
  89. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05} /* 133MHz */
  90. };
  91. /* JCENVT */
  92. static unsigned long JCENVTtbl[2][7] = {
  93. {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00}, /* 100MHz */
  94. {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  95. };
  96. /* JCACTSELS/JCACTSELM */
  97. static unsigned long JCACTSELtbl[2][7] = {
  98. {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00}, /* 100MHz */
  99. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} /* 133MHz */
  100. };
  101. static u8 scc_ide_inb(unsigned long port)
  102. {
  103. u32 data = in_be32((void*)port);
  104. return (u8)data;
  105. }
  106. static void scc_exec_command(ide_hwif_t *hwif, u8 cmd)
  107. {
  108. out_be32((void *)hwif->io_ports.command_addr, cmd);
  109. eieio();
  110. in_be32((void *)(hwif->dma_base + 0x01c));
  111. eieio();
  112. }
  113. static u8 scc_read_sff_dma_status(ide_hwif_t *hwif)
  114. {
  115. return (u8)in_be32((void *)(hwif->dma_base + 4));
  116. }
  117. static void scc_ide_insw(unsigned long port, void *addr, u32 count)
  118. {
  119. u16 *ptr = (u16 *)addr;
  120. while (count--) {
  121. *ptr++ = le16_to_cpu(in_be32((void*)port));
  122. }
  123. }
  124. static void scc_ide_insl(unsigned long port, void *addr, u32 count)
  125. {
  126. u16 *ptr = (u16 *)addr;
  127. while (count--) {
  128. *ptr++ = le16_to_cpu(in_be32((void*)port));
  129. *ptr++ = le16_to_cpu(in_be32((void*)port));
  130. }
  131. }
  132. static void scc_ide_outb(u8 addr, unsigned long port)
  133. {
  134. out_be32((void*)port, addr);
  135. }
  136. static void scc_ide_outbsync(ide_hwif_t *hwif, u8 addr, unsigned long port)
  137. {
  138. out_be32((void*)port, addr);
  139. eieio();
  140. in_be32((void*)(hwif->dma_base + 0x01c));
  141. eieio();
  142. }
  143. static void
  144. scc_ide_outsw(unsigned long port, void *addr, u32 count)
  145. {
  146. u16 *ptr = (u16 *)addr;
  147. while (count--) {
  148. out_be32((void*)port, cpu_to_le16(*ptr++));
  149. }
  150. }
  151. static void
  152. scc_ide_outsl(unsigned long port, void *addr, u32 count)
  153. {
  154. u16 *ptr = (u16 *)addr;
  155. while (count--) {
  156. out_be32((void*)port, cpu_to_le16(*ptr++));
  157. out_be32((void*)port, cpu_to_le16(*ptr++));
  158. }
  159. }
  160. /**
  161. * scc_set_pio_mode - set host controller for PIO mode
  162. * @drive: drive
  163. * @pio: PIO mode number
  164. *
  165. * Load the timing settings for this device mode into the
  166. * controller.
  167. */
  168. static void scc_set_pio_mode(ide_drive_t *drive, const u8 pio)
  169. {
  170. ide_hwif_t *hwif = HWIF(drive);
  171. struct scc_ports *ports = ide_get_hwifdata(hwif);
  172. unsigned long ctl_base = ports->ctl;
  173. unsigned long cckctrl_port = ctl_base + 0xff0;
  174. unsigned long piosht_port = ctl_base + 0x000;
  175. unsigned long pioct_port = ctl_base + 0x004;
  176. unsigned long reg;
  177. int offset;
  178. reg = in_be32((void __iomem *)cckctrl_port);
  179. if (reg & CCKCTRL_ATACLKOEN) {
  180. offset = 1; /* 133MHz */
  181. } else {
  182. offset = 0; /* 100MHz */
  183. }
  184. reg = JCHSTtbl[offset][pio] << 16 | JCHHTtbl[offset][pio];
  185. out_be32((void __iomem *)piosht_port, reg);
  186. reg = JCHCTtbl[offset][pio];
  187. out_be32((void __iomem *)pioct_port, reg);
  188. }
  189. /**
  190. * scc_set_dma_mode - set host controller for DMA mode
  191. * @drive: drive
  192. * @speed: DMA mode
  193. *
  194. * Load the timing settings for this device mode into the
  195. * controller.
  196. */
  197. static void scc_set_dma_mode(ide_drive_t *drive, const u8 speed)
  198. {
  199. ide_hwif_t *hwif = HWIF(drive);
  200. struct scc_ports *ports = ide_get_hwifdata(hwif);
  201. unsigned long ctl_base = ports->ctl;
  202. unsigned long cckctrl_port = ctl_base + 0xff0;
  203. unsigned long mdmact_port = ctl_base + 0x008;
  204. unsigned long mcrcst_port = ctl_base + 0x00c;
  205. unsigned long sdmact_port = ctl_base + 0x010;
  206. unsigned long scrcst_port = ctl_base + 0x014;
  207. unsigned long udenvt_port = ctl_base + 0x018;
  208. unsigned long tdvhsel_port = ctl_base + 0x020;
  209. int is_slave = (&hwif->drives[1] == drive);
  210. int offset, idx;
  211. unsigned long reg;
  212. unsigned long jcactsel;
  213. reg = in_be32((void __iomem *)cckctrl_port);
  214. if (reg & CCKCTRL_ATACLKOEN) {
  215. offset = 1; /* 133MHz */
  216. } else {
  217. offset = 0; /* 100MHz */
  218. }
  219. idx = speed - XFER_UDMA_0;
  220. jcactsel = JCACTSELtbl[offset][idx];
  221. if (is_slave) {
  222. out_be32((void __iomem *)sdmact_port, JCHDCTxtbl[offset][idx]);
  223. out_be32((void __iomem *)scrcst_port, JCSTWTxtbl[offset][idx]);
  224. jcactsel = jcactsel << 2;
  225. out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_SLAVE) | jcactsel);
  226. } else {
  227. out_be32((void __iomem *)mdmact_port, JCHDCTxtbl[offset][idx]);
  228. out_be32((void __iomem *)mcrcst_port, JCSTWTxtbl[offset][idx]);
  229. out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_MASTER) | jcactsel);
  230. }
  231. reg = JCTSStbl[offset][idx] << 16 | JCENVTtbl[offset][idx];
  232. out_be32((void __iomem *)udenvt_port, reg);
  233. }
  234. static void scc_dma_host_set(ide_drive_t *drive, int on)
  235. {
  236. ide_hwif_t *hwif = drive->hwif;
  237. u8 unit = (drive->select.b.unit & 0x01);
  238. u8 dma_stat = scc_ide_inb(hwif->dma_base + 4);
  239. if (on)
  240. dma_stat |= (1 << (5 + unit));
  241. else
  242. dma_stat &= ~(1 << (5 + unit));
  243. scc_ide_outb(dma_stat, hwif->dma_base + 4);
  244. }
  245. /**
  246. * scc_ide_dma_setup - begin a DMA phase
  247. * @drive: target device
  248. *
  249. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  250. * and then set up the DMA transfer registers.
  251. *
  252. * Returns 0 on success. If a PIO fallback is required then 1
  253. * is returned.
  254. */
  255. static int scc_dma_setup(ide_drive_t *drive)
  256. {
  257. ide_hwif_t *hwif = drive->hwif;
  258. struct request *rq = HWGROUP(drive)->rq;
  259. unsigned int reading;
  260. u8 dma_stat;
  261. if (rq_data_dir(rq))
  262. reading = 0;
  263. else
  264. reading = 1 << 3;
  265. /* fall back to pio! */
  266. if (!ide_build_dmatable(drive, rq)) {
  267. ide_map_sg(drive, rq);
  268. return 1;
  269. }
  270. /* PRD table */
  271. out_be32((void __iomem *)(hwif->dma_base + 8), hwif->dmatable_dma);
  272. /* specify r/w */
  273. out_be32((void __iomem *)hwif->dma_base, reading);
  274. /* read DMA status for INTR & ERROR flags */
  275. dma_stat = in_be32((void __iomem *)(hwif->dma_base + 4));
  276. /* clear INTR & ERROR flags */
  277. out_be32((void __iomem *)(hwif->dma_base + 4), dma_stat | 6);
  278. drive->waiting_for_dma = 1;
  279. return 0;
  280. }
  281. static void scc_dma_start(ide_drive_t *drive)
  282. {
  283. ide_hwif_t *hwif = drive->hwif;
  284. u8 dma_cmd = scc_ide_inb(hwif->dma_base);
  285. /* start DMA */
  286. scc_ide_outb(dma_cmd | 1, hwif->dma_base);
  287. hwif->dma = 1;
  288. wmb();
  289. }
  290. static int __scc_dma_end(ide_drive_t *drive)
  291. {
  292. ide_hwif_t *hwif = drive->hwif;
  293. u8 dma_stat, dma_cmd;
  294. drive->waiting_for_dma = 0;
  295. /* get DMA command mode */
  296. dma_cmd = scc_ide_inb(hwif->dma_base);
  297. /* stop DMA */
  298. scc_ide_outb(dma_cmd & ~1, hwif->dma_base);
  299. /* get DMA status */
  300. dma_stat = scc_ide_inb(hwif->dma_base + 4);
  301. /* clear the INTR & ERROR bits */
  302. scc_ide_outb(dma_stat | 6, hwif->dma_base + 4);
  303. /* purge DMA mappings */
  304. ide_destroy_dmatable(drive);
  305. /* verify good DMA status */
  306. hwif->dma = 0;
  307. wmb();
  308. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  309. }
  310. /**
  311. * scc_dma_end - Stop DMA
  312. * @drive: IDE drive
  313. *
  314. * Check and clear INT Status register.
  315. * Then call __scc_dma_end().
  316. */
  317. static int scc_dma_end(ide_drive_t *drive)
  318. {
  319. ide_hwif_t *hwif = HWIF(drive);
  320. void __iomem *dma_base = (void __iomem *)hwif->dma_base;
  321. unsigned long intsts_port = hwif->dma_base + 0x014;
  322. u32 reg;
  323. int dma_stat, data_loss = 0;
  324. static int retry = 0;
  325. /* errata A308 workaround: Step5 (check data loss) */
  326. /* We don't check non ide_disk because it is limited to UDMA4 */
  327. if (!(in_be32((void __iomem *)hwif->io_ports.ctl_addr)
  328. & ERR_STAT) &&
  329. drive->media == ide_disk && drive->current_speed > XFER_UDMA_4) {
  330. reg = in_be32((void __iomem *)intsts_port);
  331. if (!(reg & INTSTS_ACTEINT)) {
  332. printk(KERN_WARNING "%s: operation failed (transfer data loss)\n",
  333. drive->name);
  334. data_loss = 1;
  335. if (retry++) {
  336. struct request *rq = HWGROUP(drive)->rq;
  337. int unit;
  338. /* ERROR_RESET and drive->crc_count are needed
  339. * to reduce DMA transfer mode in retry process.
  340. */
  341. if (rq)
  342. rq->errors |= ERROR_RESET;
  343. for (unit = 0; unit < MAX_DRIVES; unit++) {
  344. ide_drive_t *drive = &hwif->drives[unit];
  345. drive->crc_count++;
  346. }
  347. }
  348. }
  349. }
  350. while (1) {
  351. reg = in_be32((void __iomem *)intsts_port);
  352. if (reg & INTSTS_SERROR) {
  353. printk(KERN_WARNING "%s: SERROR\n", SCC_PATA_NAME);
  354. out_be32((void __iomem *)intsts_port, INTSTS_SERROR|INTSTS_BMSINT);
  355. out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
  356. continue;
  357. }
  358. if (reg & INTSTS_PRERR) {
  359. u32 maea0, maec0;
  360. unsigned long ctl_base = hwif->config_data;
  361. maea0 = in_be32((void __iomem *)(ctl_base + 0xF50));
  362. maec0 = in_be32((void __iomem *)(ctl_base + 0xF54));
  363. printk(KERN_WARNING "%s: PRERR [addr:%x cmd:%x]\n", SCC_PATA_NAME, maea0, maec0);
  364. out_be32((void __iomem *)intsts_port, INTSTS_PRERR|INTSTS_BMSINT);
  365. out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
  366. continue;
  367. }
  368. if (reg & INTSTS_RERR) {
  369. printk(KERN_WARNING "%s: Response Error\n", SCC_PATA_NAME);
  370. out_be32((void __iomem *)intsts_port, INTSTS_RERR|INTSTS_BMSINT);
  371. out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
  372. continue;
  373. }
  374. if (reg & INTSTS_ICERR) {
  375. out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
  376. printk(KERN_WARNING "%s: Illegal Configuration\n", SCC_PATA_NAME);
  377. out_be32((void __iomem *)intsts_port, INTSTS_ICERR|INTSTS_BMSINT);
  378. continue;
  379. }
  380. if (reg & INTSTS_BMSINT) {
  381. printk(KERN_WARNING "%s: Internal Bus Error\n", SCC_PATA_NAME);
  382. out_be32((void __iomem *)intsts_port, INTSTS_BMSINT);
  383. ide_do_reset(drive);
  384. continue;
  385. }
  386. if (reg & INTSTS_BMHE) {
  387. out_be32((void __iomem *)intsts_port, INTSTS_BMHE);
  388. continue;
  389. }
  390. if (reg & INTSTS_ACTEINT) {
  391. out_be32((void __iomem *)intsts_port, INTSTS_ACTEINT);
  392. continue;
  393. }
  394. if (reg & INTSTS_IOIRQS) {
  395. out_be32((void __iomem *)intsts_port, INTSTS_IOIRQS);
  396. continue;
  397. }
  398. break;
  399. }
  400. dma_stat = __scc_dma_end(drive);
  401. if (data_loss)
  402. dma_stat |= 2; /* emulate DMA error (to retry command) */
  403. return dma_stat;
  404. }
  405. /* returns 1 if dma irq issued, 0 otherwise */
  406. static int scc_dma_test_irq(ide_drive_t *drive)
  407. {
  408. ide_hwif_t *hwif = HWIF(drive);
  409. u32 int_stat = in_be32((void __iomem *)hwif->dma_base + 0x014);
  410. /* SCC errata A252,A308 workaround: Step4 */
  411. if ((in_be32((void __iomem *)hwif->io_ports.ctl_addr)
  412. & ERR_STAT) &&
  413. (int_stat & INTSTS_INTRQ))
  414. return 1;
  415. /* SCC errata A308 workaround: Step5 (polling IOIRQS) */
  416. if (int_stat & INTSTS_IOIRQS)
  417. return 1;
  418. if (!drive->waiting_for_dma)
  419. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  420. drive->name, __func__);
  421. return 0;
  422. }
  423. static u8 scc_udma_filter(ide_drive_t *drive)
  424. {
  425. ide_hwif_t *hwif = drive->hwif;
  426. u8 mask = hwif->ultra_mask;
  427. /* errata A308 workaround: limit non ide_disk drive to UDMA4 */
  428. if ((drive->media != ide_disk) && (mask & 0xE0)) {
  429. printk(KERN_INFO "%s: limit %s to UDMA4\n",
  430. SCC_PATA_NAME, drive->name);
  431. mask = ATA_UDMA4;
  432. }
  433. return mask;
  434. }
  435. /**
  436. * setup_mmio_scc - map CTRL/BMID region
  437. * @dev: PCI device we are configuring
  438. * @name: device name
  439. *
  440. */
  441. static int setup_mmio_scc (struct pci_dev *dev, const char *name)
  442. {
  443. unsigned long ctl_base = pci_resource_start(dev, 0);
  444. unsigned long dma_base = pci_resource_start(dev, 1);
  445. unsigned long ctl_size = pci_resource_len(dev, 0);
  446. unsigned long dma_size = pci_resource_len(dev, 1);
  447. void __iomem *ctl_addr;
  448. void __iomem *dma_addr;
  449. int i, ret;
  450. for (i = 0; i < MAX_HWIFS; i++) {
  451. if (scc_ports[i].ctl == 0)
  452. break;
  453. }
  454. if (i >= MAX_HWIFS)
  455. return -ENOMEM;
  456. ret = pci_request_selected_regions(dev, (1 << 2) - 1, name);
  457. if (ret < 0) {
  458. printk(KERN_ERR "%s: can't reserve resources\n", name);
  459. return ret;
  460. }
  461. if ((ctl_addr = ioremap(ctl_base, ctl_size)) == NULL)
  462. goto fail_0;
  463. if ((dma_addr = ioremap(dma_base, dma_size)) == NULL)
  464. goto fail_1;
  465. pci_set_master(dev);
  466. scc_ports[i].ctl = (unsigned long)ctl_addr;
  467. scc_ports[i].dma = (unsigned long)dma_addr;
  468. pci_set_drvdata(dev, (void *) &scc_ports[i]);
  469. return 1;
  470. fail_1:
  471. iounmap(ctl_addr);
  472. fail_0:
  473. return -ENOMEM;
  474. }
  475. static int scc_ide_setup_pci_device(struct pci_dev *dev,
  476. const struct ide_port_info *d)
  477. {
  478. struct scc_ports *ports = pci_get_drvdata(dev);
  479. ide_hwif_t *hwif = NULL;
  480. hw_regs_t hw, *hws[] = { &hw, NULL, NULL, NULL };
  481. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  482. int i;
  483. hwif = ide_find_port_slot(d);
  484. if (hwif == NULL)
  485. return -ENOMEM;
  486. memset(&hw, 0, sizeof(hw));
  487. for (i = 0; i <= 8; i++)
  488. hw.io_ports_array[i] = ports->dma + 0x20 + i * 4;
  489. hw.irq = dev->irq;
  490. hw.dev = &dev->dev;
  491. hw.chipset = ide_pci;
  492. idx[0] = hwif->index;
  493. ide_device_add(idx, d, hws);
  494. return 0;
  495. }
  496. /**
  497. * init_setup_scc - set up an SCC PATA Controller
  498. * @dev: PCI device
  499. * @d: IDE port info
  500. *
  501. * Perform the initial set up for this device.
  502. */
  503. static int __devinit init_setup_scc(struct pci_dev *dev,
  504. const struct ide_port_info *d)
  505. {
  506. unsigned long ctl_base;
  507. unsigned long dma_base;
  508. unsigned long cckctrl_port;
  509. unsigned long intmask_port;
  510. unsigned long mode_port;
  511. unsigned long ecmode_port;
  512. unsigned long dma_status_port;
  513. u32 reg = 0;
  514. struct scc_ports *ports;
  515. int rc;
  516. rc = pci_enable_device(dev);
  517. if (rc)
  518. goto end;
  519. rc = setup_mmio_scc(dev, d->name);
  520. if (rc < 0)
  521. goto end;
  522. ports = pci_get_drvdata(dev);
  523. ctl_base = ports->ctl;
  524. dma_base = ports->dma;
  525. cckctrl_port = ctl_base + 0xff0;
  526. intmask_port = dma_base + 0x010;
  527. mode_port = ctl_base + 0x024;
  528. ecmode_port = ctl_base + 0xf00;
  529. dma_status_port = dma_base + 0x004;
  530. /* controller initialization */
  531. reg = 0;
  532. out_be32((void*)cckctrl_port, reg);
  533. reg |= CCKCTRL_ATACLKOEN;
  534. out_be32((void*)cckctrl_port, reg);
  535. reg |= CCKCTRL_LCLKEN | CCKCTRL_OCLKEN;
  536. out_be32((void*)cckctrl_port, reg);
  537. reg |= CCKCTRL_CRST;
  538. out_be32((void*)cckctrl_port, reg);
  539. for (;;) {
  540. reg = in_be32((void*)cckctrl_port);
  541. if (reg & CCKCTRL_CRST)
  542. break;
  543. udelay(5000);
  544. }
  545. reg |= CCKCTRL_ATARESET;
  546. out_be32((void*)cckctrl_port, reg);
  547. out_be32((void*)ecmode_port, ECMODE_VALUE);
  548. out_be32((void*)mode_port, MODE_JCUSFEN);
  549. out_be32((void*)intmask_port, INTMASK_MSK);
  550. rc = scc_ide_setup_pci_device(dev, d);
  551. end:
  552. return rc;
  553. }
  554. static void scc_tf_load(ide_drive_t *drive, ide_task_t *task)
  555. {
  556. struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  557. struct ide_taskfile *tf = &task->tf;
  558. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  559. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  560. HIHI = 0xFF;
  561. if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  562. out_be32((void *)io_ports->data_addr,
  563. (tf->hob_data << 8) | tf->data);
  564. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  565. scc_ide_outb(tf->hob_feature, io_ports->feature_addr);
  566. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  567. scc_ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  568. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  569. scc_ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  570. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  571. scc_ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  572. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  573. scc_ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  574. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  575. scc_ide_outb(tf->feature, io_ports->feature_addr);
  576. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  577. scc_ide_outb(tf->nsect, io_ports->nsect_addr);
  578. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  579. scc_ide_outb(tf->lbal, io_ports->lbal_addr);
  580. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  581. scc_ide_outb(tf->lbam, io_ports->lbam_addr);
  582. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  583. scc_ide_outb(tf->lbah, io_ports->lbah_addr);
  584. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  585. scc_ide_outb((tf->device & HIHI) | drive->select.all,
  586. io_ports->device_addr);
  587. }
  588. static void scc_tf_read(ide_drive_t *drive, ide_task_t *task)
  589. {
  590. struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  591. struct ide_taskfile *tf = &task->tf;
  592. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  593. u16 data = (u16)in_be32((void *)io_ports->data_addr);
  594. tf->data = data & 0xff;
  595. tf->hob_data = (data >> 8) & 0xff;
  596. }
  597. /* be sure we're looking at the low order bits */
  598. scc_ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  599. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  600. tf->nsect = scc_ide_inb(io_ports->nsect_addr);
  601. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  602. tf->lbal = scc_ide_inb(io_ports->lbal_addr);
  603. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  604. tf->lbam = scc_ide_inb(io_ports->lbam_addr);
  605. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  606. tf->lbah = scc_ide_inb(io_ports->lbah_addr);
  607. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  608. tf->device = scc_ide_inb(io_ports->device_addr);
  609. if (task->tf_flags & IDE_TFLAG_LBA48) {
  610. scc_ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  611. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  612. tf->hob_feature = scc_ide_inb(io_ports->feature_addr);
  613. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  614. tf->hob_nsect = scc_ide_inb(io_ports->nsect_addr);
  615. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  616. tf->hob_lbal = scc_ide_inb(io_ports->lbal_addr);
  617. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  618. tf->hob_lbam = scc_ide_inb(io_ports->lbam_addr);
  619. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  620. tf->hob_lbah = scc_ide_inb(io_ports->lbah_addr);
  621. }
  622. }
  623. static void scc_input_data(ide_drive_t *drive, struct request *rq,
  624. void *buf, unsigned int len)
  625. {
  626. unsigned long data_addr = drive->hwif->io_ports.data_addr;
  627. len++;
  628. if (drive->io_32bit) {
  629. scc_ide_insl(data_addr, buf, len / 4);
  630. if ((len & 3) >= 2)
  631. scc_ide_insw(data_addr, (u8 *)buf + (len & ~3), 1);
  632. } else
  633. scc_ide_insw(data_addr, buf, len / 2);
  634. }
  635. static void scc_output_data(ide_drive_t *drive, struct request *rq,
  636. void *buf, unsigned int len)
  637. {
  638. unsigned long data_addr = drive->hwif->io_ports.data_addr;
  639. len++;
  640. if (drive->io_32bit) {
  641. scc_ide_outsl(data_addr, buf, len / 4);
  642. if ((len & 3) >= 2)
  643. scc_ide_outsw(data_addr, (u8 *)buf + (len & ~3), 1);
  644. } else
  645. scc_ide_outsw(data_addr, buf, len / 2);
  646. }
  647. /**
  648. * init_mmio_iops_scc - set up the iops for MMIO
  649. * @hwif: interface to set up
  650. *
  651. */
  652. static void __devinit init_mmio_iops_scc(ide_hwif_t *hwif)
  653. {
  654. struct pci_dev *dev = to_pci_dev(hwif->dev);
  655. struct scc_ports *ports = pci_get_drvdata(dev);
  656. unsigned long dma_base = ports->dma;
  657. ide_set_hwifdata(hwif, ports);
  658. hwif->exec_command = scc_exec_command;
  659. hwif->read_sff_dma_status = scc_read_sff_dma_status;
  660. hwif->tf_load = scc_tf_load;
  661. hwif->tf_read = scc_tf_read;
  662. hwif->input_data = scc_input_data;
  663. hwif->output_data = scc_output_data;
  664. hwif->INB = scc_ide_inb;
  665. hwif->OUTB = scc_ide_outb;
  666. hwif->OUTBSYNC = scc_ide_outbsync;
  667. hwif->dma_base = dma_base;
  668. hwif->config_data = ports->ctl;
  669. }
  670. /**
  671. * init_iops_scc - set up iops
  672. * @hwif: interface to set up
  673. *
  674. * Do the basic setup for the SCC hardware interface
  675. * and then do the MMIO setup.
  676. */
  677. static void __devinit init_iops_scc(ide_hwif_t *hwif)
  678. {
  679. struct pci_dev *dev = to_pci_dev(hwif->dev);
  680. hwif->hwif_data = NULL;
  681. if (pci_get_drvdata(dev) == NULL)
  682. return;
  683. init_mmio_iops_scc(hwif);
  684. }
  685. static u8 __devinit scc_cable_detect(ide_hwif_t *hwif)
  686. {
  687. return ATA_CBL_PATA80;
  688. }
  689. /**
  690. * init_hwif_scc - set up hwif
  691. * @hwif: interface to set up
  692. *
  693. * We do the basic set up of the interface structure. The SCC
  694. * requires several custom handlers so we override the default
  695. * ide DMA handlers appropriately.
  696. */
  697. static void __devinit init_hwif_scc(ide_hwif_t *hwif)
  698. {
  699. struct scc_ports *ports = ide_get_hwifdata(hwif);
  700. ports->hwif = hwif;
  701. /* PTERADD */
  702. out_be32((void __iomem *)(hwif->dma_base + 0x018), hwif->dmatable_dma);
  703. if (in_be32((void __iomem *)(hwif->config_data + 0xff0)) & CCKCTRL_ATACLKOEN)
  704. hwif->ultra_mask = ATA_UDMA6; /* 133MHz */
  705. else
  706. hwif->ultra_mask = ATA_UDMA5; /* 100MHz */
  707. }
  708. static const struct ide_port_ops scc_port_ops = {
  709. .set_pio_mode = scc_set_pio_mode,
  710. .set_dma_mode = scc_set_dma_mode,
  711. .udma_filter = scc_udma_filter,
  712. .cable_detect = scc_cable_detect,
  713. };
  714. static const struct ide_dma_ops scc_dma_ops = {
  715. .dma_host_set = scc_dma_host_set,
  716. .dma_setup = scc_dma_setup,
  717. .dma_exec_cmd = ide_dma_exec_cmd,
  718. .dma_start = scc_dma_start,
  719. .dma_end = scc_dma_end,
  720. .dma_test_irq = scc_dma_test_irq,
  721. .dma_lost_irq = ide_dma_lost_irq,
  722. .dma_timeout = ide_dma_timeout,
  723. };
  724. #define DECLARE_SCC_DEV(name_str) \
  725. { \
  726. .name = name_str, \
  727. .init_iops = init_iops_scc, \
  728. .init_hwif = init_hwif_scc, \
  729. .port_ops = &scc_port_ops, \
  730. .dma_ops = &scc_dma_ops, \
  731. .host_flags = IDE_HFLAG_SINGLE, \
  732. .pio_mask = ATA_PIO4, \
  733. }
  734. static const struct ide_port_info scc_chipsets[] __devinitdata = {
  735. /* 0 */ DECLARE_SCC_DEV("sccIDE"),
  736. };
  737. /**
  738. * scc_init_one - pci layer discovery entry
  739. * @dev: PCI device
  740. * @id: ident table entry
  741. *
  742. * Called by the PCI code when it finds an SCC PATA controller.
  743. * We then use the IDE PCI generic helper to do most of the work.
  744. */
  745. static int __devinit scc_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  746. {
  747. return init_setup_scc(dev, &scc_chipsets[id->driver_data]);
  748. }
  749. /**
  750. * scc_remove - pci layer remove entry
  751. * @dev: PCI device
  752. *
  753. * Called by the PCI code when it removes an SCC PATA controller.
  754. */
  755. static void __devexit scc_remove(struct pci_dev *dev)
  756. {
  757. struct scc_ports *ports = pci_get_drvdata(dev);
  758. ide_hwif_t *hwif = ports->hwif;
  759. if (hwif->dmatable_cpu) {
  760. pci_free_consistent(dev, PRD_ENTRIES * PRD_BYTES,
  761. hwif->dmatable_cpu, hwif->dmatable_dma);
  762. hwif->dmatable_cpu = NULL;
  763. }
  764. ide_unregister(hwif);
  765. iounmap((void*)ports->dma);
  766. iounmap((void*)ports->ctl);
  767. pci_release_selected_regions(dev, (1 << 2) - 1);
  768. memset(ports, 0, sizeof(*ports));
  769. }
  770. static const struct pci_device_id scc_pci_tbl[] = {
  771. { PCI_VDEVICE(TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SCC_ATA), 0 },
  772. { 0, },
  773. };
  774. MODULE_DEVICE_TABLE(pci, scc_pci_tbl);
  775. static struct pci_driver driver = {
  776. .name = "SCC IDE",
  777. .id_table = scc_pci_tbl,
  778. .probe = scc_init_one,
  779. .remove = scc_remove,
  780. };
  781. static int scc_ide_init(void)
  782. {
  783. return ide_pci_register_driver(&driver);
  784. }
  785. module_init(scc_ide_init);
  786. /* -- No exit code?
  787. static void scc_ide_exit(void)
  788. {
  789. ide_pci_unregister_driver(&driver);
  790. }
  791. module_exit(scc_ide_exit);
  792. */
  793. MODULE_DESCRIPTION("PCI driver module for Toshiba SCC IDE");
  794. MODULE_LICENSE("GPL");