pata_at91.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * PATA driver for AT91SAM9260 Static Memory Controller
  3. * with CompactFlash interface in True IDE mode
  4. *
  5. * Copyright (C) 2009 Matyukevich Sergey
  6. *
  7. * Based on:
  8. * * generic platform driver by Paul Mundt: drivers/ata/pata_platform.c
  9. * * pata_at32 driver by Kristoffer Nyborg Gregertsen
  10. * * at91_ide driver by Stanislaw Gruszka
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2
  14. * as published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/gfp.h>
  22. #include <scsi/scsi_host.h>
  23. #include <linux/ata.h>
  24. #include <linux/clk.h>
  25. #include <linux/libata.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/ata_platform.h>
  28. #include <mach/at91sam9_smc.h>
  29. #include <mach/board.h>
  30. #include <mach/gpio.h>
  31. #define DRV_NAME "pata_at91"
  32. #define DRV_VERSION "0.2"
  33. #define CF_IDE_OFFSET 0x00c00000
  34. #define CF_ALT_IDE_OFFSET 0x00e00000
  35. #define CF_IDE_RES_SIZE 0x08
  36. #define NCS_RD_PULSE_LIMIT 0x3f /* maximal value for pulse bitfields */
  37. struct at91_ide_info {
  38. unsigned long mode;
  39. unsigned int cs;
  40. struct clk *mck;
  41. void __iomem *ide_addr;
  42. void __iomem *alt_addr;
  43. };
  44. static const struct ata_timing initial_timing =
  45. {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
  46. static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
  47. {
  48. unsigned long mul;
  49. /*
  50. * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
  51. * x * (f / 1_000_000_000) =
  52. * x * ((f * 65536) / 1_000_000_000) / 65536 =
  53. * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
  54. */
  55. mul = (mck_hz / 10000) << 16;
  56. mul /= 100000;
  57. return (ns * mul + 65536) >> 16; /* rounding */
  58. }
  59. static void set_smc_mode(struct at91_ide_info *info)
  60. {
  61. at91_sys_write(AT91_SMC_MODE(info->cs), info->mode);
  62. return;
  63. }
  64. static void set_smc_timing(struct device *dev,
  65. struct at91_ide_info *info, const struct ata_timing *ata)
  66. {
  67. unsigned long read_cycle, write_cycle, active, recover;
  68. unsigned long nrd_setup, nrd_pulse, nrd_recover;
  69. unsigned long nwe_setup, nwe_pulse;
  70. unsigned long ncs_write_setup, ncs_write_pulse;
  71. unsigned long ncs_read_setup, ncs_read_pulse;
  72. unsigned long mck_hz;
  73. read_cycle = ata->cyc8b;
  74. nrd_setup = ata->setup;
  75. nrd_pulse = ata->act8b;
  76. nrd_recover = ata->rec8b;
  77. mck_hz = clk_get_rate(info->mck);
  78. read_cycle = calc_mck_cycles(read_cycle, mck_hz);
  79. nrd_setup = calc_mck_cycles(nrd_setup, mck_hz);
  80. nrd_pulse = calc_mck_cycles(nrd_pulse, mck_hz);
  81. nrd_recover = calc_mck_cycles(nrd_recover, mck_hz);
  82. active = nrd_setup + nrd_pulse;
  83. recover = read_cycle - active;
  84. /* Need at least two cycles recovery */
  85. if (recover < 2)
  86. read_cycle = active + 2;
  87. /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
  88. ncs_read_setup = 1;
  89. ncs_read_pulse = read_cycle - 2;
  90. if (ncs_read_pulse > NCS_RD_PULSE_LIMIT) {
  91. ncs_read_pulse = NCS_RD_PULSE_LIMIT;
  92. dev_warn(dev, "ncs_read_pulse limited to maximal value %lu\n",
  93. ncs_read_pulse);
  94. }
  95. /* Write timings same as read timings */
  96. write_cycle = read_cycle;
  97. nwe_setup = nrd_setup;
  98. nwe_pulse = nrd_pulse;
  99. ncs_write_setup = ncs_read_setup;
  100. ncs_write_pulse = ncs_read_pulse;
  101. dev_dbg(dev, "ATA timings: nrd_setup = %lu nrd_pulse = %lu nrd_cycle = %lu\n",
  102. nrd_setup, nrd_pulse, read_cycle);
  103. dev_dbg(dev, "ATA timings: nwe_setup = %lu nwe_pulse = %lu nwe_cycle = %lu\n",
  104. nwe_setup, nwe_pulse, write_cycle);
  105. dev_dbg(dev, "ATA timings: ncs_read_setup = %lu ncs_read_pulse = %lu\n",
  106. ncs_read_setup, ncs_read_pulse);
  107. dev_dbg(dev, "ATA timings: ncs_write_setup = %lu ncs_write_pulse = %lu\n",
  108. ncs_write_setup, ncs_write_pulse);
  109. at91_sys_write(AT91_SMC_SETUP(info->cs),
  110. AT91_SMC_NWESETUP_(nwe_setup) |
  111. AT91_SMC_NRDSETUP_(nrd_setup) |
  112. AT91_SMC_NCS_WRSETUP_(ncs_write_setup) |
  113. AT91_SMC_NCS_RDSETUP_(ncs_read_setup));
  114. at91_sys_write(AT91_SMC_PULSE(info->cs),
  115. AT91_SMC_NWEPULSE_(nwe_pulse) |
  116. AT91_SMC_NRDPULSE_(nrd_pulse) |
  117. AT91_SMC_NCS_WRPULSE_(ncs_write_pulse) |
  118. AT91_SMC_NCS_RDPULSE_(ncs_read_pulse));
  119. at91_sys_write(AT91_SMC_CYCLE(info->cs),
  120. AT91_SMC_NWECYCLE_(write_cycle) |
  121. AT91_SMC_NRDCYCLE_(read_cycle));
  122. return;
  123. }
  124. static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
  125. {
  126. struct at91_ide_info *info = ap->host->private_data;
  127. struct ata_timing timing;
  128. int ret;
  129. /* Compute ATA timing and set it to SMC */
  130. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  131. if (ret) {
  132. dev_warn(ap->dev, "Failed to compute ATA timing %d, "
  133. "set PIO_0 timing\n", ret);
  134. set_smc_timing(ap->dev, info, &initial_timing);
  135. } else {
  136. set_smc_timing(ap->dev, info, &timing);
  137. }
  138. /* Setup SMC mode */
  139. set_smc_mode(info);
  140. return;
  141. }
  142. static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
  143. unsigned char *buf, unsigned int buflen, int rw)
  144. {
  145. struct at91_ide_info *info = dev->link->ap->host->private_data;
  146. unsigned int consumed;
  147. unsigned long flags;
  148. unsigned int mode;
  149. local_irq_save(flags);
  150. mode = at91_sys_read(AT91_SMC_MODE(info->cs));
  151. /* set 16bit mode before writing data */
  152. at91_sys_write(AT91_SMC_MODE(info->cs),
  153. (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_16);
  154. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  155. /* restore 8bit mode after data is written */
  156. at91_sys_write(AT91_SMC_MODE(info->cs),
  157. (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_8);
  158. local_irq_restore(flags);
  159. return consumed;
  160. }
  161. static struct scsi_host_template pata_at91_sht = {
  162. ATA_PIO_SHT(DRV_NAME),
  163. };
  164. static struct ata_port_operations pata_at91_port_ops = {
  165. .inherits = &ata_sff_port_ops,
  166. .sff_data_xfer = pata_at91_data_xfer_noirq,
  167. .set_piomode = pata_at91_set_piomode,
  168. .cable_detect = ata_cable_40wire,
  169. };
  170. static int __devinit pata_at91_probe(struct platform_device *pdev)
  171. {
  172. struct at91_cf_data *board = pdev->dev.platform_data;
  173. struct device *dev = &pdev->dev;
  174. struct at91_ide_info *info;
  175. struct resource *mem_res;
  176. struct ata_host *host;
  177. struct ata_port *ap;
  178. int irq_flags = 0;
  179. int irq = 0;
  180. int ret;
  181. /* get platform resources: IO/CTL memories and irq/rst pins */
  182. if (pdev->num_resources != 1) {
  183. dev_err(&pdev->dev, "invalid number of resources\n");
  184. return -EINVAL;
  185. }
  186. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  187. if (!mem_res) {
  188. dev_err(dev, "failed to get mem resource\n");
  189. return -EINVAL;
  190. }
  191. irq = board->irq_pin;
  192. /* init ata host */
  193. host = ata_host_alloc(dev, 1);
  194. if (!host)
  195. return -ENOMEM;
  196. ap = host->ports[0];
  197. ap->ops = &pata_at91_port_ops;
  198. ap->flags |= ATA_FLAG_SLAVE_POSS;
  199. ap->pio_mask = ATA_PIO4;
  200. if (!irq) {
  201. ap->flags |= ATA_FLAG_PIO_POLLING;
  202. ata_port_desc(ap, "no IRQ, using PIO polling");
  203. }
  204. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  205. if (!info) {
  206. dev_err(dev, "failed to allocate memory for private data\n");
  207. return -ENOMEM;
  208. }
  209. info->mck = clk_get(NULL, "mck");
  210. if (IS_ERR(info->mck)) {
  211. dev_err(dev, "failed to get access to mck clock\n");
  212. return -ENODEV;
  213. }
  214. info->cs = board->chipselect;
  215. info->mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  216. AT91_SMC_EXNWMODE_READY | AT91_SMC_BAT_SELECT |
  217. AT91_SMC_DBW_8 | AT91_SMC_TDF_(0);
  218. info->ide_addr = devm_ioremap(dev,
  219. mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
  220. if (!info->ide_addr) {
  221. dev_err(dev, "failed to map IO base\n");
  222. ret = -ENOMEM;
  223. goto err_put;
  224. }
  225. info->alt_addr = devm_ioremap(dev,
  226. mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
  227. if (!info->alt_addr) {
  228. dev_err(dev, "failed to map CTL base\n");
  229. ret = -ENOMEM;
  230. goto err_put;
  231. }
  232. ap->ioaddr.cmd_addr = info->ide_addr;
  233. ap->ioaddr.ctl_addr = info->alt_addr + 0x06;
  234. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  235. ata_sff_std_ports(&ap->ioaddr);
  236. ata_port_desc(ap, "mmio cmd 0x%llx ctl 0x%llx",
  237. (unsigned long long)mem_res->start + CF_IDE_OFFSET,
  238. (unsigned long long)mem_res->start + CF_ALT_IDE_OFFSET);
  239. host->private_data = info;
  240. return ata_host_activate(host, irq ? gpio_to_irq(irq) : 0,
  241. irq ? ata_sff_interrupt : NULL,
  242. irq_flags, &pata_at91_sht);
  243. err_put:
  244. clk_put(info->mck);
  245. return ret;
  246. }
  247. static int __devexit pata_at91_remove(struct platform_device *pdev)
  248. {
  249. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  250. struct at91_ide_info *info;
  251. if (!host)
  252. return 0;
  253. info = host->private_data;
  254. ata_host_detach(host);
  255. if (!info)
  256. return 0;
  257. clk_put(info->mck);
  258. return 0;
  259. }
  260. static struct platform_driver pata_at91_driver = {
  261. .probe = pata_at91_probe,
  262. .remove = __devexit_p(pata_at91_remove),
  263. .driver = {
  264. .name = DRV_NAME,
  265. .owner = THIS_MODULE,
  266. },
  267. };
  268. static int __init pata_at91_init(void)
  269. {
  270. return platform_driver_register(&pata_at91_driver);
  271. }
  272. static void __exit pata_at91_exit(void)
  273. {
  274. platform_driver_unregister(&pata_at91_driver);
  275. }
  276. module_init(pata_at91_init);
  277. module_exit(pata_at91_exit);
  278. MODULE_LICENSE("GPL");
  279. MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
  280. MODULE_AUTHOR("Matyukevich Sergey");
  281. MODULE_VERSION(DRV_VERSION);