at91_ide.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * IDE host driver for AT91 (SAM9, CAP9, AT572D940HF) Static Memory Controller
  3. * with Compact Flash True IDE logic
  4. *
  5. * Copyright (c) 2008, 2009 Kelvatek Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/ide.h>
  27. #include <linux/platform_device.h>
  28. #include <mach/board.h>
  29. #include <mach/gpio.h>
  30. #include <mach/at91sam9263.h>
  31. #include <mach/at91sam9_smc.h>
  32. #include <mach/at91sam9263_matrix.h>
  33. #define DRV_NAME "at91_ide"
  34. #define perr(fmt, args...) pr_err(DRV_NAME ": " fmt, ##args)
  35. #define pdbg(fmt, args...) pr_debug("%s " fmt, __func__, ##args)
  36. /*
  37. * Access to IDE device is possible through EBI Static Memory Controller
  38. * with Compact Flash logic. For details see EBI and SMC datasheet sections
  39. * of any microcontroller from AT91SAM9 family.
  40. *
  41. * Within SMC chip select address space, lines A[23:21] distinguish Compact
  42. * Flash modes (I/O, common memory, attribute memory, True IDE). IDE modes are:
  43. * 0x00c0000 - True IDE
  44. * 0x00e0000 - Alternate True IDE (Alt Status Register)
  45. *
  46. * On True IDE mode Task File and Data Register are mapped at the same address.
  47. * To distinguish access between these two different bus data width is used:
  48. * 8Bit for Task File, 16Bit for Data I/O.
  49. *
  50. * After initialization we do 8/16 bit flipping (changes in SMC MODE register)
  51. * only inside IDE callback routines which are serialized by IDE layer,
  52. * so no additional locking needed.
  53. */
  54. #define TASK_FILE 0x00c00000
  55. #define ALT_MODE 0x00e00000
  56. #define REGS_SIZE 8
  57. #define enter_16bit(cs, mode) do { \
  58. mode = at91_sys_read(AT91_SMC_MODE(cs)); \
  59. at91_sys_write(AT91_SMC_MODE(cs), mode | AT91_SMC_DBW_16); \
  60. } while (0)
  61. #define leave_16bit(cs, mode) at91_sys_write(AT91_SMC_MODE(cs), mode);
  62. static void set_smc_timings(const u8 chipselect, const u16 cycle,
  63. const u16 setup, const u16 pulse,
  64. const u16 data_float, int use_iordy)
  65. {
  66. unsigned long mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  67. AT91_SMC_BAT_SELECT;
  68. /* disable or enable waiting for IORDY signal */
  69. if (use_iordy)
  70. mode |= AT91_SMC_EXNWMODE_READY;
  71. /* add data float cycles if needed */
  72. if (data_float)
  73. mode |= AT91_SMC_TDF_(data_float);
  74. at91_sys_write(AT91_SMC_MODE(chipselect), mode);
  75. /* setup timings in SMC */
  76. at91_sys_write(AT91_SMC_SETUP(chipselect), AT91_SMC_NWESETUP_(setup) |
  77. AT91_SMC_NCS_WRSETUP_(0) |
  78. AT91_SMC_NRDSETUP_(setup) |
  79. AT91_SMC_NCS_RDSETUP_(0));
  80. at91_sys_write(AT91_SMC_PULSE(chipselect), AT91_SMC_NWEPULSE_(pulse) |
  81. AT91_SMC_NCS_WRPULSE_(cycle) |
  82. AT91_SMC_NRDPULSE_(pulse) |
  83. AT91_SMC_NCS_RDPULSE_(cycle));
  84. at91_sys_write(AT91_SMC_CYCLE(chipselect), AT91_SMC_NWECYCLE_(cycle) |
  85. AT91_SMC_NRDCYCLE_(cycle));
  86. }
  87. static unsigned int calc_mck_cycles(unsigned int ns, unsigned int mck_hz)
  88. {
  89. u64 tmp = ns;
  90. tmp *= mck_hz;
  91. tmp += 1000*1000*1000 - 1; /* round up */
  92. do_div(tmp, 1000*1000*1000);
  93. return (unsigned int) tmp;
  94. }
  95. static void apply_timings(const u8 chipselect, const u8 pio,
  96. const struct ide_timing *timing, int use_iordy)
  97. {
  98. unsigned int t0, t1, t2, t6z;
  99. unsigned int cycle, setup, pulse, data_float;
  100. unsigned int mck_hz;
  101. struct clk *mck;
  102. /* see table 22 of Compact Flash standard 4.1 for the meaning,
  103. * we do not stretch active (t2) time, so setup (t1) + hold time (th)
  104. * assure at least minimal recovery (t2i) time */
  105. t0 = timing->cyc8b;
  106. t1 = timing->setup;
  107. t2 = timing->act8b;
  108. t6z = (pio < 5) ? 30 : 20;
  109. pdbg("t0=%u t1=%u t2=%u t6z=%u\n", t0, t1, t2, t6z);
  110. mck = clk_get(NULL, "mck");
  111. BUG_ON(IS_ERR(mck));
  112. mck_hz = clk_get_rate(mck);
  113. pdbg("mck_hz=%u\n", mck_hz);
  114. cycle = calc_mck_cycles(t0, mck_hz);
  115. setup = calc_mck_cycles(t1, mck_hz);
  116. pulse = calc_mck_cycles(t2, mck_hz);
  117. data_float = calc_mck_cycles(t6z, mck_hz);
  118. pdbg("cycle=%u setup=%u pulse=%u data_float=%u\n",
  119. cycle, setup, pulse, data_float);
  120. set_smc_timings(chipselect, cycle, setup, pulse, data_float, use_iordy);
  121. }
  122. static void at91_ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd,
  123. void *buf, unsigned int len)
  124. {
  125. ide_hwif_t *hwif = drive->hwif;
  126. struct ide_io_ports *io_ports = &hwif->io_ports;
  127. u8 chipselect = hwif->select_data;
  128. unsigned long mode;
  129. pdbg("cs %u buf %p len %d\n", chipselect, buf, len);
  130. len++;
  131. enter_16bit(chipselect, mode);
  132. readsw((void __iomem *)io_ports->data_addr, buf, len / 2);
  133. leave_16bit(chipselect, mode);
  134. }
  135. static void at91_ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd,
  136. void *buf, unsigned int len)
  137. {
  138. ide_hwif_t *hwif = drive->hwif;
  139. struct ide_io_ports *io_ports = &hwif->io_ports;
  140. u8 chipselect = hwif->select_data;
  141. unsigned long mode;
  142. pdbg("cs %u buf %p len %d\n", chipselect, buf, len);
  143. enter_16bit(chipselect, mode);
  144. writesw((void __iomem *)io_ports->data_addr, buf, len / 2);
  145. leave_16bit(chipselect, mode);
  146. }
  147. static void at91_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  148. {
  149. struct ide_timing *timing;
  150. u8 chipselect = drive->hwif->select_data;
  151. int use_iordy = 0;
  152. pdbg("chipselect %u pio %u\n", chipselect, pio);
  153. timing = ide_timing_find_mode(XFER_PIO_0 + pio);
  154. BUG_ON(!timing);
  155. if (ide_pio_need_iordy(drive, pio))
  156. use_iordy = 1;
  157. apply_timings(chipselect, pio, timing, use_iordy);
  158. }
  159. static const struct ide_tp_ops at91_ide_tp_ops = {
  160. .exec_command = ide_exec_command,
  161. .read_status = ide_read_status,
  162. .read_altstatus = ide_read_altstatus,
  163. .write_devctl = ide_write_devctl,
  164. .dev_select = ide_dev_select,
  165. .tf_load = ide_tf_load,
  166. .tf_read = ide_tf_read,
  167. .input_data = at91_ide_input_data,
  168. .output_data = at91_ide_output_data,
  169. };
  170. static const struct ide_port_ops at91_ide_port_ops = {
  171. .set_pio_mode = at91_ide_set_pio_mode,
  172. };
  173. static const struct ide_port_info at91_ide_port_info __initdata = {
  174. .port_ops = &at91_ide_port_ops,
  175. .tp_ops = &at91_ide_tp_ops,
  176. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA | IDE_HFLAG_SINGLE |
  177. IDE_HFLAG_NO_IO_32BIT | IDE_HFLAG_UNMASK_IRQS,
  178. .pio_mask = ATA_PIO6,
  179. .chipset = ide_generic,
  180. };
  181. /*
  182. * If interrupt is delivered through GPIO, IRQ are triggered on falling
  183. * and rising edge of signal. Whereas IDE device request interrupt on high
  184. * level (rising edge in our case). This mean we have fake interrupts, so
  185. * we need to check interrupt pin and exit instantly from ISR when line
  186. * is on low level.
  187. */
  188. irqreturn_t at91_irq_handler(int irq, void *dev_id)
  189. {
  190. int ntries = 8;
  191. int pin_val1, pin_val2;
  192. /* additional deglitch, line can be noisy in badly designed PCB */
  193. do {
  194. pin_val1 = at91_get_gpio_value(irq);
  195. pin_val2 = at91_get_gpio_value(irq);
  196. } while (pin_val1 != pin_val2 && --ntries > 0);
  197. if (pin_val1 == 0 || ntries <= 0)
  198. return IRQ_HANDLED;
  199. return ide_intr(irq, dev_id);
  200. }
  201. static int __init at91_ide_probe(struct platform_device *pdev)
  202. {
  203. int ret;
  204. struct ide_hw hw, *hws[] = { &hw };
  205. struct ide_host *host;
  206. struct resource *res;
  207. unsigned long tf_base = 0, ctl_base = 0;
  208. struct at91_cf_data *board = pdev->dev.platform_data;
  209. if (!board)
  210. return -ENODEV;
  211. if (board->det_pin && at91_get_gpio_value(board->det_pin) != 0) {
  212. perr("no device detected\n");
  213. return -ENODEV;
  214. }
  215. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  216. if (!res) {
  217. perr("can't get memory resource\n");
  218. return -ENODEV;
  219. }
  220. if (!devm_request_mem_region(&pdev->dev, res->start + TASK_FILE,
  221. REGS_SIZE, "ide") ||
  222. !devm_request_mem_region(&pdev->dev, res->start + ALT_MODE,
  223. REGS_SIZE, "alt")) {
  224. perr("memory resources in use\n");
  225. return -EBUSY;
  226. }
  227. pdbg("chipselect %u irq %u res %08lx\n", board->chipselect,
  228. board->irq_pin, (unsigned long) res->start);
  229. tf_base = (unsigned long) devm_ioremap(&pdev->dev, res->start + TASK_FILE,
  230. REGS_SIZE);
  231. ctl_base = (unsigned long) devm_ioremap(&pdev->dev, res->start + ALT_MODE,
  232. REGS_SIZE);
  233. if (!tf_base || !ctl_base) {
  234. perr("can't map memory regions\n");
  235. return -EBUSY;
  236. }
  237. memset(&hw, 0, sizeof(hw));
  238. if (board->flags & AT91_IDE_SWAP_A0_A2) {
  239. /* workaround for stupid hardware bug */
  240. hw.io_ports.data_addr = tf_base + 0;
  241. hw.io_ports.error_addr = tf_base + 4;
  242. hw.io_ports.nsect_addr = tf_base + 2;
  243. hw.io_ports.lbal_addr = tf_base + 6;
  244. hw.io_ports.lbam_addr = tf_base + 1;
  245. hw.io_ports.lbah_addr = tf_base + 5;
  246. hw.io_ports.device_addr = tf_base + 3;
  247. hw.io_ports.command_addr = tf_base + 7;
  248. hw.io_ports.ctl_addr = ctl_base + 3;
  249. } else
  250. ide_std_init_ports(&hw, tf_base, ctl_base + 6);
  251. hw.irq = board->irq_pin;
  252. hw.dev = &pdev->dev;
  253. host = ide_host_alloc(&at91_ide_port_info, hws, 1);
  254. if (!host) {
  255. perr("failed to allocate ide host\n");
  256. return -ENOMEM;
  257. }
  258. /* setup Static Memory Controller - PIO 0 as default */
  259. apply_timings(board->chipselect, 0, ide_timing_find_mode(XFER_PIO_0), 0);
  260. /* with GPIO interrupt we have to do quirks in handler */
  261. if (board->irq_pin >= PIN_BASE)
  262. host->irq_handler = at91_irq_handler;
  263. host->ports[0]->select_data = board->chipselect;
  264. ret = ide_host_register(host, &at91_ide_port_info, hws);
  265. if (ret) {
  266. perr("failed to register ide host\n");
  267. goto err_free_host;
  268. }
  269. platform_set_drvdata(pdev, host);
  270. return 0;
  271. err_free_host:
  272. ide_host_free(host);
  273. return ret;
  274. }
  275. static int __exit at91_ide_remove(struct platform_device *pdev)
  276. {
  277. struct ide_host *host = platform_get_drvdata(pdev);
  278. ide_host_remove(host);
  279. return 0;
  280. }
  281. static struct platform_driver at91_ide_driver = {
  282. .driver = {
  283. .name = DRV_NAME,
  284. .owner = THIS_MODULE,
  285. },
  286. .remove = __exit_p(at91_ide_remove),
  287. };
  288. static int __init at91_ide_init(void)
  289. {
  290. return platform_driver_probe(&at91_ide_driver, at91_ide_probe);
  291. }
  292. static void __exit at91_ide_exit(void)
  293. {
  294. platform_driver_unregister(&at91_ide_driver);
  295. }
  296. module_init(at91_ide_init);
  297. module_exit(at91_ide_exit);
  298. MODULE_LICENSE("GPL");
  299. MODULE_AUTHOR("Stanislaw Gruszka <stf_xl@wp.pl>");