icside.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (c) 1996-2004 Russell King.
  3. *
  4. * Please note that this platform does not support 32-bit IDE IO.
  5. */
  6. #include <linux/string.h>
  7. #include <linux/module.h>
  8. #include <linux/ioport.h>
  9. #include <linux/slab.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/errno.h>
  12. #include <linux/ide.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/io.h>
  18. #include <asm/dma.h>
  19. #include <asm/ecard.h>
  20. #define DRV_NAME "icside"
  21. #define ICS_IDENT_OFFSET 0x2280
  22. #define ICS_ARCIN_V5_INTRSTAT 0x0000
  23. #define ICS_ARCIN_V5_INTROFFSET 0x0004
  24. #define ICS_ARCIN_V5_IDEOFFSET 0x2800
  25. #define ICS_ARCIN_V5_IDEALTOFFSET 0x2b80
  26. #define ICS_ARCIN_V5_IDESTEPPING 6
  27. #define ICS_ARCIN_V6_IDEOFFSET_1 0x2000
  28. #define ICS_ARCIN_V6_INTROFFSET_1 0x2200
  29. #define ICS_ARCIN_V6_INTRSTAT_1 0x2290
  30. #define ICS_ARCIN_V6_IDEALTOFFSET_1 0x2380
  31. #define ICS_ARCIN_V6_IDEOFFSET_2 0x3000
  32. #define ICS_ARCIN_V6_INTROFFSET_2 0x3200
  33. #define ICS_ARCIN_V6_INTRSTAT_2 0x3290
  34. #define ICS_ARCIN_V6_IDEALTOFFSET_2 0x3380
  35. #define ICS_ARCIN_V6_IDESTEPPING 6
  36. struct cardinfo {
  37. unsigned int dataoffset;
  38. unsigned int ctrloffset;
  39. unsigned int stepping;
  40. };
  41. static struct cardinfo icside_cardinfo_v5 = {
  42. .dataoffset = ICS_ARCIN_V5_IDEOFFSET,
  43. .ctrloffset = ICS_ARCIN_V5_IDEALTOFFSET,
  44. .stepping = ICS_ARCIN_V5_IDESTEPPING,
  45. };
  46. static struct cardinfo icside_cardinfo_v6_1 = {
  47. .dataoffset = ICS_ARCIN_V6_IDEOFFSET_1,
  48. .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_1,
  49. .stepping = ICS_ARCIN_V6_IDESTEPPING,
  50. };
  51. static struct cardinfo icside_cardinfo_v6_2 = {
  52. .dataoffset = ICS_ARCIN_V6_IDEOFFSET_2,
  53. .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_2,
  54. .stepping = ICS_ARCIN_V6_IDESTEPPING,
  55. };
  56. struct icside_state {
  57. void __iomem *irq_port;
  58. void __iomem *ioc_base;
  59. unsigned int sel;
  60. unsigned int type;
  61. struct ide_host *host;
  62. };
  63. #define ICS_TYPE_A3IN 0
  64. #define ICS_TYPE_A3USER 1
  65. #define ICS_TYPE_V6 3
  66. #define ICS_TYPE_V5 15
  67. #define ICS_TYPE_NOTYPE ((unsigned int)-1)
  68. /* ---------------- Version 5 PCB Support Functions --------------------- */
  69. /* Prototype: icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
  70. * Purpose : enable interrupts from card
  71. */
  72. static void icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
  73. {
  74. struct icside_state *state = ec->irq_data;
  75. writeb(0, state->irq_port + ICS_ARCIN_V5_INTROFFSET);
  76. }
  77. /* Prototype: icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
  78. * Purpose : disable interrupts from card
  79. */
  80. static void icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
  81. {
  82. struct icside_state *state = ec->irq_data;
  83. readb(state->irq_port + ICS_ARCIN_V5_INTROFFSET);
  84. }
  85. static const expansioncard_ops_t icside_ops_arcin_v5 = {
  86. .irqenable = icside_irqenable_arcin_v5,
  87. .irqdisable = icside_irqdisable_arcin_v5,
  88. };
  89. /* ---------------- Version 6 PCB Support Functions --------------------- */
  90. /* Prototype: icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
  91. * Purpose : enable interrupts from card
  92. */
  93. static void icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
  94. {
  95. struct icside_state *state = ec->irq_data;
  96. void __iomem *base = state->irq_port;
  97. writeb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
  98. readb(base + ICS_ARCIN_V6_INTROFFSET_2);
  99. writeb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
  100. readb(base + ICS_ARCIN_V6_INTROFFSET_1);
  101. }
  102. /* Prototype: icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
  103. * Purpose : disable interrupts from card
  104. */
  105. static void icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
  106. {
  107. struct icside_state *state = ec->irq_data;
  108. readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
  109. readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
  110. }
  111. /* Prototype: icside_irqprobe(struct expansion_card *ec)
  112. * Purpose : detect an active interrupt from card
  113. */
  114. static int icside_irqpending_arcin_v6(struct expansion_card *ec)
  115. {
  116. struct icside_state *state = ec->irq_data;
  117. return readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
  118. readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
  119. }
  120. static const expansioncard_ops_t icside_ops_arcin_v6 = {
  121. .irqenable = icside_irqenable_arcin_v6,
  122. .irqdisable = icside_irqdisable_arcin_v6,
  123. .irqpending = icside_irqpending_arcin_v6,
  124. };
  125. #ifdef CONFIG_BLK_DEV_IDEDMA_ICS
  126. /*
  127. * SG-DMA support.
  128. *
  129. * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
  130. * There is only one DMA controller per card, which means that only
  131. * one drive can be accessed at one time. NOTE! We do not enforce that
  132. * here, but we rely on the main IDE driver spotting that both
  133. * interfaces use the same IRQ, which should guarantee this.
  134. */
  135. /*
  136. * Configure the IOMD to give the appropriate timings for the transfer
  137. * mode being requested. We take the advice of the ATA standards, and
  138. * calculate the cycle time based on the transfer mode, and the EIDE
  139. * MW DMA specs that the drive provides in the IDENTIFY command.
  140. *
  141. * We have the following IOMD DMA modes to choose from:
  142. *
  143. * Type Active Recovery Cycle
  144. * A 250 (250) 312 (550) 562 (800)
  145. * B 187 250 437
  146. * C 125 (125) 125 (375) 250 (500)
  147. * D 62 125 187
  148. *
  149. * (figures in brackets are actual measured timings)
  150. *
  151. * However, we also need to take care of the read/write active and
  152. * recovery timings:
  153. *
  154. * Read Write
  155. * Mode Active -- Recovery -- Cycle IOMD type
  156. * MW0 215 50 215 480 A
  157. * MW1 80 50 50 150 C
  158. * MW2 70 25 25 120 C
  159. */
  160. static void icside_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  161. {
  162. unsigned long cycle_time;
  163. int use_dma_info = 0;
  164. const u8 xfer_mode = drive->dma_mode;
  165. switch (xfer_mode) {
  166. case XFER_MW_DMA_2:
  167. cycle_time = 250;
  168. use_dma_info = 1;
  169. break;
  170. case XFER_MW_DMA_1:
  171. cycle_time = 250;
  172. use_dma_info = 1;
  173. break;
  174. case XFER_MW_DMA_0:
  175. cycle_time = 480;
  176. break;
  177. case XFER_SW_DMA_2:
  178. case XFER_SW_DMA_1:
  179. case XFER_SW_DMA_0:
  180. cycle_time = 480;
  181. break;
  182. }
  183. /*
  184. * If we're going to be doing MW_DMA_1 or MW_DMA_2, we should
  185. * take care to note the values in the ID...
  186. */
  187. if (use_dma_info && drive->id[ATA_ID_EIDE_DMA_TIME] > cycle_time)
  188. cycle_time = drive->id[ATA_ID_EIDE_DMA_TIME];
  189. ide_set_drivedata(drive, (void *)cycle_time);
  190. printk("%s: %s selected (peak %dMB/s)\n", drive->name,
  191. ide_xfer_verbose(xfer_mode),
  192. 2000 / (unsigned long)ide_get_drivedata(drive));
  193. }
  194. static const struct ide_port_ops icside_v6_port_ops = {
  195. .set_dma_mode = icside_set_dma_mode,
  196. };
  197. static void icside_dma_host_set(ide_drive_t *drive, int on)
  198. {
  199. }
  200. static int icside_dma_end(ide_drive_t *drive)
  201. {
  202. ide_hwif_t *hwif = drive->hwif;
  203. struct expansion_card *ec = ECARD_DEV(hwif->dev);
  204. disable_dma(ec->dma);
  205. return get_dma_residue(ec->dma) != 0;
  206. }
  207. static void icside_dma_start(ide_drive_t *drive)
  208. {
  209. ide_hwif_t *hwif = drive->hwif;
  210. struct expansion_card *ec = ECARD_DEV(hwif->dev);
  211. /* We can not enable DMA on both channels simultaneously. */
  212. BUG_ON(dma_channel_active(ec->dma));
  213. enable_dma(ec->dma);
  214. }
  215. static int icside_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
  216. {
  217. ide_hwif_t *hwif = drive->hwif;
  218. struct expansion_card *ec = ECARD_DEV(hwif->dev);
  219. struct icside_state *state = ecard_get_drvdata(ec);
  220. unsigned int dma_mode;
  221. if (cmd->tf_flags & IDE_TFLAG_WRITE)
  222. dma_mode = DMA_MODE_WRITE;
  223. else
  224. dma_mode = DMA_MODE_READ;
  225. /*
  226. * We can not enable DMA on both channels.
  227. */
  228. BUG_ON(dma_channel_active(ec->dma));
  229. /*
  230. * Route the DMA signals to the correct interface.
  231. */
  232. writeb(state->sel | hwif->channel, state->ioc_base);
  233. /*
  234. * Select the correct timing for this drive.
  235. */
  236. set_dma_speed(ec->dma, (unsigned long)ide_get_drivedata(drive));
  237. /*
  238. * Tell the DMA engine about the SG table and
  239. * data direction.
  240. */
  241. set_dma_sg(ec->dma, hwif->sg_table, cmd->sg_nents);
  242. set_dma_mode(ec->dma, dma_mode);
  243. return 0;
  244. }
  245. static int icside_dma_test_irq(ide_drive_t *drive)
  246. {
  247. ide_hwif_t *hwif = drive->hwif;
  248. struct expansion_card *ec = ECARD_DEV(hwif->dev);
  249. struct icside_state *state = ecard_get_drvdata(ec);
  250. return readb(state->irq_port +
  251. (hwif->channel ?
  252. ICS_ARCIN_V6_INTRSTAT_2 :
  253. ICS_ARCIN_V6_INTRSTAT_1)) & 1;
  254. }
  255. static int icside_dma_init(ide_hwif_t *hwif, const struct ide_port_info *d)
  256. {
  257. hwif->dmatable_cpu = NULL;
  258. hwif->dmatable_dma = 0;
  259. return 0;
  260. }
  261. static const struct ide_dma_ops icside_v6_dma_ops = {
  262. .dma_host_set = icside_dma_host_set,
  263. .dma_setup = icside_dma_setup,
  264. .dma_start = icside_dma_start,
  265. .dma_end = icside_dma_end,
  266. .dma_test_irq = icside_dma_test_irq,
  267. .dma_lost_irq = ide_dma_lost_irq,
  268. };
  269. #else
  270. #define icside_v6_dma_ops NULL
  271. #endif
  272. static int icside_dma_off_init(ide_hwif_t *hwif, const struct ide_port_info *d)
  273. {
  274. return -EOPNOTSUPP;
  275. }
  276. static void icside_setup_ports(struct ide_hw *hw, void __iomem *base,
  277. struct cardinfo *info, struct expansion_card *ec)
  278. {
  279. unsigned long port = (unsigned long)base + info->dataoffset;
  280. hw->io_ports.data_addr = port;
  281. hw->io_ports.error_addr = port + (1 << info->stepping);
  282. hw->io_ports.nsect_addr = port + (2 << info->stepping);
  283. hw->io_ports.lbal_addr = port + (3 << info->stepping);
  284. hw->io_ports.lbam_addr = port + (4 << info->stepping);
  285. hw->io_ports.lbah_addr = port + (5 << info->stepping);
  286. hw->io_ports.device_addr = port + (6 << info->stepping);
  287. hw->io_ports.status_addr = port + (7 << info->stepping);
  288. hw->io_ports.ctl_addr = (unsigned long)base + info->ctrloffset;
  289. hw->irq = ec->irq;
  290. hw->dev = &ec->dev;
  291. }
  292. static const struct ide_port_info icside_v5_port_info = {
  293. .host_flags = IDE_HFLAG_NO_DMA,
  294. .chipset = ide_acorn,
  295. };
  296. static int __devinit
  297. icside_register_v5(struct icside_state *state, struct expansion_card *ec)
  298. {
  299. void __iomem *base;
  300. struct ide_host *host;
  301. struct ide_hw hw, *hws[] = { &hw };
  302. int ret;
  303. base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
  304. if (!base)
  305. return -ENOMEM;
  306. state->irq_port = base;
  307. ec->irqaddr = base + ICS_ARCIN_V5_INTRSTAT;
  308. ec->irqmask = 1;
  309. ecard_setirq(ec, &icside_ops_arcin_v5, state);
  310. /*
  311. * Be on the safe side - disable interrupts
  312. */
  313. icside_irqdisable_arcin_v5(ec, 0);
  314. icside_setup_ports(&hw, base, &icside_cardinfo_v5, ec);
  315. host = ide_host_alloc(&icside_v5_port_info, hws, 1);
  316. if (host == NULL)
  317. return -ENODEV;
  318. state->host = host;
  319. ecard_set_drvdata(ec, state);
  320. ret = ide_host_register(host, &icside_v5_port_info, hws);
  321. if (ret)
  322. goto err_free;
  323. return 0;
  324. err_free:
  325. ide_host_free(host);
  326. ecard_set_drvdata(ec, NULL);
  327. return ret;
  328. }
  329. static const struct ide_port_info icside_v6_port_info __initdata = {
  330. .init_dma = icside_dma_off_init,
  331. .dma_ops = &icside_v6_dma_ops,
  332. .host_flags = IDE_HFLAG_SERIALIZE | IDE_HFLAG_MMIO,
  333. .mwdma_mask = ATA_MWDMA2,
  334. .swdma_mask = ATA_SWDMA2,
  335. .chipset = ide_acorn,
  336. };
  337. static int __devinit
  338. icside_register_v6(struct icside_state *state, struct expansion_card *ec)
  339. {
  340. void __iomem *ioc_base, *easi_base;
  341. struct ide_host *host;
  342. unsigned int sel = 0;
  343. int ret;
  344. struct ide_hw hw[2], *hws[] = { &hw[0], &hw[1] };
  345. struct ide_port_info d = icside_v6_port_info;
  346. ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
  347. if (!ioc_base) {
  348. ret = -ENOMEM;
  349. goto out;
  350. }
  351. easi_base = ioc_base;
  352. if (ecard_resource_flags(ec, ECARD_RES_EASI)) {
  353. easi_base = ecardm_iomap(ec, ECARD_RES_EASI, 0, 0);
  354. if (!easi_base) {
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. /*
  359. * Enable access to the EASI region.
  360. */
  361. sel = 1 << 5;
  362. }
  363. writeb(sel, ioc_base);
  364. ecard_setirq(ec, &icside_ops_arcin_v6, state);
  365. state->irq_port = easi_base;
  366. state->ioc_base = ioc_base;
  367. state->sel = sel;
  368. /*
  369. * Be on the safe side - disable interrupts
  370. */
  371. icside_irqdisable_arcin_v6(ec, 0);
  372. icside_setup_ports(&hw[0], easi_base, &icside_cardinfo_v6_1, ec);
  373. icside_setup_ports(&hw[1], easi_base, &icside_cardinfo_v6_2, ec);
  374. host = ide_host_alloc(&d, hws, 2);
  375. if (host == NULL)
  376. return -ENODEV;
  377. state->host = host;
  378. ecard_set_drvdata(ec, state);
  379. if (ec->dma != NO_DMA && !request_dma(ec->dma, DRV_NAME)) {
  380. d.init_dma = icside_dma_init;
  381. d.port_ops = &icside_v6_port_ops;
  382. d.dma_ops = NULL;
  383. }
  384. ret = ide_host_register(host, &d, hws);
  385. if (ret)
  386. goto err_free;
  387. return 0;
  388. err_free:
  389. ide_host_free(host);
  390. if (d.dma_ops)
  391. free_dma(ec->dma);
  392. ecard_set_drvdata(ec, NULL);
  393. out:
  394. return ret;
  395. }
  396. static int __devinit
  397. icside_probe(struct expansion_card *ec, const struct ecard_id *id)
  398. {
  399. struct icside_state *state;
  400. void __iomem *idmem;
  401. int ret;
  402. ret = ecard_request_resources(ec);
  403. if (ret)
  404. goto out;
  405. state = kzalloc(sizeof(struct icside_state), GFP_KERNEL);
  406. if (!state) {
  407. ret = -ENOMEM;
  408. goto release;
  409. }
  410. state->type = ICS_TYPE_NOTYPE;
  411. idmem = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
  412. if (idmem) {
  413. unsigned int type;
  414. type = readb(idmem + ICS_IDENT_OFFSET) & 1;
  415. type |= (readb(idmem + ICS_IDENT_OFFSET + 4) & 1) << 1;
  416. type |= (readb(idmem + ICS_IDENT_OFFSET + 8) & 1) << 2;
  417. type |= (readb(idmem + ICS_IDENT_OFFSET + 12) & 1) << 3;
  418. ecardm_iounmap(ec, idmem);
  419. state->type = type;
  420. }
  421. switch (state->type) {
  422. case ICS_TYPE_A3IN:
  423. dev_warn(&ec->dev, "A3IN unsupported\n");
  424. ret = -ENODEV;
  425. break;
  426. case ICS_TYPE_A3USER:
  427. dev_warn(&ec->dev, "A3USER unsupported\n");
  428. ret = -ENODEV;
  429. break;
  430. case ICS_TYPE_V5:
  431. ret = icside_register_v5(state, ec);
  432. break;
  433. case ICS_TYPE_V6:
  434. ret = icside_register_v6(state, ec);
  435. break;
  436. default:
  437. dev_warn(&ec->dev, "unknown interface type\n");
  438. ret = -ENODEV;
  439. break;
  440. }
  441. if (ret == 0)
  442. goto out;
  443. kfree(state);
  444. release:
  445. ecard_release_resources(ec);
  446. out:
  447. return ret;
  448. }
  449. static void __devexit icside_remove(struct expansion_card *ec)
  450. {
  451. struct icside_state *state = ecard_get_drvdata(ec);
  452. switch (state->type) {
  453. case ICS_TYPE_V5:
  454. /* FIXME: tell IDE to stop using the interface */
  455. /* Disable interrupts */
  456. icside_irqdisable_arcin_v5(ec, 0);
  457. break;
  458. case ICS_TYPE_V6:
  459. /* FIXME: tell IDE to stop using the interface */
  460. if (ec->dma != NO_DMA)
  461. free_dma(ec->dma);
  462. /* Disable interrupts */
  463. icside_irqdisable_arcin_v6(ec, 0);
  464. /* Reset the ROM pointer/EASI selection */
  465. writeb(0, state->ioc_base);
  466. break;
  467. }
  468. ecard_set_drvdata(ec, NULL);
  469. kfree(state);
  470. ecard_release_resources(ec);
  471. }
  472. static void icside_shutdown(struct expansion_card *ec)
  473. {
  474. struct icside_state *state = ecard_get_drvdata(ec);
  475. unsigned long flags;
  476. /*
  477. * Disable interrupts from this card. We need to do
  478. * this before disabling EASI since we may be accessing
  479. * this register via that region.
  480. */
  481. local_irq_save(flags);
  482. ec->ops->irqdisable(ec, 0);
  483. local_irq_restore(flags);
  484. /*
  485. * Reset the ROM pointer so that we can read the ROM
  486. * after a soft reboot. This also disables access to
  487. * the IDE taskfile via the EASI region.
  488. */
  489. if (state->ioc_base)
  490. writeb(0, state->ioc_base);
  491. }
  492. static const struct ecard_id icside_ids[] = {
  493. { MANU_ICS, PROD_ICS_IDE },
  494. { MANU_ICS2, PROD_ICS2_IDE },
  495. { 0xffff, 0xffff }
  496. };
  497. static struct ecard_driver icside_driver = {
  498. .probe = icside_probe,
  499. .remove = __devexit_p(icside_remove),
  500. .shutdown = icside_shutdown,
  501. .id_table = icside_ids,
  502. .drv = {
  503. .name = "icside",
  504. },
  505. };
  506. static int __init icside_init(void)
  507. {
  508. return ecard_register_driver(&icside_driver);
  509. }
  510. static void __exit icside_exit(void)
  511. {
  512. ecard_remove_driver(&icside_driver);
  513. }
  514. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  515. MODULE_LICENSE("GPL");
  516. MODULE_DESCRIPTION("ICS IDE driver");
  517. module_init(icside_init);
  518. module_exit(icside_exit);