pata_at91.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. * 2011 Igor Plyatov
  7. *
  8. * Based on:
  9. * * generic platform driver by Paul Mundt: drivers/ata/pata_platform.c
  10. * * pata_at32 driver by Kristoffer Nyborg Gregertsen
  11. * * at91_ide driver by Stanislaw Gruszka
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.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/mfd/syscon.h>
  27. #include <linux/mfd/syscon/atmel-smc.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/ata_platform.h>
  30. #include <linux/platform_data/atmel.h>
  31. #include <linux/regmap.h>
  32. #include <linux/gpio.h>
  33. #define DRV_NAME "pata_at91"
  34. #define DRV_VERSION "0.3"
  35. #define CF_IDE_OFFSET 0x00c00000
  36. #define CF_ALT_IDE_OFFSET 0x00e00000
  37. #define CF_IDE_RES_SIZE 0x08
  38. #define CS_PULSE_MAXIMUM 319
  39. #define ER_SMC_CALC 1
  40. #define ER_SMC_RECALC 2
  41. struct at91_ide_info {
  42. unsigned long mode;
  43. unsigned int cs;
  44. struct clk *mck;
  45. void __iomem *ide_addr;
  46. void __iomem *alt_addr;
  47. };
  48. /**
  49. * struct smc_range - range of valid values for SMC register.
  50. */
  51. struct smc_range {
  52. int min;
  53. int max;
  54. };
  55. struct regmap *smc;
  56. struct at91sam9_smc_generic_fields {
  57. struct regmap_field *setup;
  58. struct regmap_field *pulse;
  59. struct regmap_field *cycle;
  60. struct regmap_field *mode;
  61. } fields;
  62. /**
  63. * adjust_smc_value - adjust value for one of SMC registers.
  64. * @value: adjusted value
  65. * @range: array of SMC ranges with valid values
  66. * @size: SMC ranges array size
  67. *
  68. * This returns the difference between input and output value or negative
  69. * in case of invalid input value.
  70. * If negative returned, then output value = maximal possible from ranges.
  71. */
  72. static int adjust_smc_value(int *value, struct smc_range *range, int size)
  73. {
  74. int maximum = (range + size - 1)->max;
  75. int remainder;
  76. do {
  77. if (*value < range->min) {
  78. remainder = range->min - *value;
  79. *value = range->min; /* nearest valid value */
  80. return remainder;
  81. } else if ((range->min <= *value) && (*value <= range->max))
  82. return 0;
  83. range++;
  84. } while (--size);
  85. *value = maximum;
  86. return -1; /* invalid value */
  87. }
  88. /**
  89. * calc_smc_vals - calculate SMC register values
  90. * @dev: ATA device
  91. * @setup: SMC_SETUP register value
  92. * @pulse: SMC_PULSE register value
  93. * @cycle: SMC_CYCLE register value
  94. *
  95. * This returns negative in case of invalid values for SMC registers:
  96. * -ER_SMC_RECALC - recalculation required for SMC values,
  97. * -ER_SMC_CALC - calculation failed (invalid input values).
  98. *
  99. * SMC use special coding scheme, see "Coding and Range of Timing
  100. * Parameters" table from AT91SAM9 datasheets.
  101. *
  102. * SMC_SETUP = 128*setup[5] + setup[4:0]
  103. * SMC_PULSE = 256*pulse[6] + pulse[5:0]
  104. * SMC_CYCLE = 256*cycle[8:7] + cycle[6:0]
  105. */
  106. static int calc_smc_vals(struct device *dev,
  107. int *setup, int *pulse, int *cycle, int *cs_pulse)
  108. {
  109. int ret_val;
  110. int err = 0;
  111. struct smc_range range_setup[] = { /* SMC_SETUP valid values */
  112. {.min = 0, .max = 31}, /* first range */
  113. {.min = 128, .max = 159} /* second range */
  114. };
  115. struct smc_range range_pulse[] = { /* SMC_PULSE valid values */
  116. {.min = 0, .max = 63}, /* first range */
  117. {.min = 256, .max = 319} /* second range */
  118. };
  119. struct smc_range range_cycle[] = { /* SMC_CYCLE valid values */
  120. {.min = 0, .max = 127}, /* first range */
  121. {.min = 256, .max = 383}, /* second range */
  122. {.min = 512, .max = 639}, /* third range */
  123. {.min = 768, .max = 895} /* fourth range */
  124. };
  125. ret_val = adjust_smc_value(setup, range_setup, ARRAY_SIZE(range_setup));
  126. if (ret_val < 0)
  127. dev_warn(dev, "maximal SMC Setup value\n");
  128. else
  129. *cycle += ret_val;
  130. ret_val = adjust_smc_value(pulse, range_pulse, ARRAY_SIZE(range_pulse));
  131. if (ret_val < 0)
  132. dev_warn(dev, "maximal SMC Pulse value\n");
  133. else
  134. *cycle += ret_val;
  135. ret_val = adjust_smc_value(cycle, range_cycle, ARRAY_SIZE(range_cycle));
  136. if (ret_val < 0)
  137. dev_warn(dev, "maximal SMC Cycle value\n");
  138. *cs_pulse = *cycle;
  139. if (*cs_pulse > CS_PULSE_MAXIMUM) {
  140. dev_err(dev, "unable to calculate valid SMC settings\n");
  141. return -ER_SMC_CALC;
  142. }
  143. ret_val = adjust_smc_value(cs_pulse, range_pulse,
  144. ARRAY_SIZE(range_pulse));
  145. if (ret_val < 0) {
  146. dev_warn(dev, "maximal SMC CS Pulse value\n");
  147. } else if (ret_val != 0) {
  148. *cycle = *cs_pulse;
  149. dev_warn(dev, "SMC Cycle extended\n");
  150. err = -ER_SMC_RECALC;
  151. }
  152. return err;
  153. }
  154. /**
  155. * to_smc_format - convert values into SMC format
  156. * @setup: SETUP value of SMC Setup Register
  157. * @pulse: PULSE value of SMC Pulse Register
  158. * @cycle: CYCLE value of SMC Cycle Register
  159. * @cs_pulse: NCS_PULSE value of SMC Pulse Register
  160. */
  161. static void to_smc_format(int *setup, int *pulse, int *cycle, int *cs_pulse)
  162. {
  163. *setup = (*setup & 0x1f) | ((*setup & 0x80) >> 2);
  164. *pulse = (*pulse & 0x3f) | ((*pulse & 0x100) >> 2);
  165. *cycle = (*cycle & 0x7f) | ((*cycle & 0x300) >> 1);
  166. *cs_pulse = (*cs_pulse & 0x3f) | ((*cs_pulse & 0x100) >> 2);
  167. }
  168. static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
  169. {
  170. unsigned long mul;
  171. /*
  172. * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
  173. * x * (f / 1_000_000_000) =
  174. * x * ((f * 65536) / 1_000_000_000) / 65536 =
  175. * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
  176. */
  177. mul = (mck_hz / 10000) << 16;
  178. mul /= 100000;
  179. return (ns * mul + 65536) >> 16; /* rounding */
  180. }
  181. /**
  182. * set_smc_timing - SMC timings setup.
  183. * @dev: device
  184. * @info: AT91 IDE info
  185. * @ata: ATA timings
  186. *
  187. * Its assumed that write timings are same as read timings,
  188. * cs_setup = 0 and cs_pulse = cycle.
  189. */
  190. static void set_smc_timing(struct device *dev, struct ata_device *adev,
  191. struct at91_ide_info *info, const struct ata_timing *ata)
  192. {
  193. int ret = 0;
  194. int use_iordy;
  195. unsigned int t6z; /* data tristate time in ns */
  196. unsigned int cycle; /* SMC Cycle width in MCK ticks */
  197. unsigned int setup; /* SMC Setup width in MCK ticks */
  198. unsigned int pulse; /* CFIOR and CFIOW pulse width in MCK ticks */
  199. unsigned int cs_pulse; /* CS4 or CS5 pulse width in MCK ticks*/
  200. unsigned int tdf_cycles; /* SMC TDF MCK ticks */
  201. unsigned long mck_hz; /* MCK frequency in Hz */
  202. t6z = (ata->mode < XFER_PIO_5) ? 30 : 20;
  203. mck_hz = clk_get_rate(info->mck);
  204. cycle = calc_mck_cycles(ata->cyc8b, mck_hz);
  205. setup = calc_mck_cycles(ata->setup, mck_hz);
  206. pulse = calc_mck_cycles(ata->act8b, mck_hz);
  207. tdf_cycles = calc_mck_cycles(t6z, mck_hz);
  208. do {
  209. ret = calc_smc_vals(dev, &setup, &pulse, &cycle, &cs_pulse);
  210. } while (ret == -ER_SMC_RECALC);
  211. if (ret == -ER_SMC_CALC)
  212. dev_err(dev, "Interface may not operate correctly\n");
  213. dev_dbg(dev, "SMC Setup=%u, Pulse=%u, Cycle=%u, CS Pulse=%u\n",
  214. setup, pulse, cycle, cs_pulse);
  215. to_smc_format(&setup, &pulse, &cycle, &cs_pulse);
  216. /* disable or enable waiting for IORDY signal */
  217. use_iordy = ata_pio_need_iordy(adev);
  218. if (use_iordy)
  219. info->mode |= AT91_SMC_EXNWMODE_READY;
  220. if (tdf_cycles > 15) {
  221. tdf_cycles = 15;
  222. dev_warn(dev, "maximal SMC TDF Cycles value\n");
  223. }
  224. dev_dbg(dev, "Use IORDY=%u, TDF Cycles=%u\n", use_iordy, tdf_cycles);
  225. regmap_fields_write(fields.setup, info->cs,
  226. AT91SAM9_SMC_NRDSETUP(setup) |
  227. AT91SAM9_SMC_NWESETUP(setup) |
  228. AT91SAM9_SMC_NCS_NRDSETUP(0) |
  229. AT91SAM9_SMC_NCS_WRSETUP(0));
  230. regmap_fields_write(fields.pulse, info->cs,
  231. AT91SAM9_SMC_NRDPULSE(pulse) |
  232. AT91SAM9_SMC_NWEPULSE(pulse) |
  233. AT91SAM9_SMC_NCS_NRDPULSE(cs_pulse) |
  234. AT91SAM9_SMC_NCS_WRPULSE(cs_pulse));
  235. regmap_fields_write(fields.cycle, info->cs,
  236. AT91SAM9_SMC_NRDCYCLE(cycle) |
  237. AT91SAM9_SMC_NWECYCLE(cycle));
  238. regmap_fields_write(fields.mode, info->cs, info->mode |
  239. AT91_SMC_TDF_(tdf_cycles));
  240. }
  241. static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
  242. {
  243. struct at91_ide_info *info = ap->host->private_data;
  244. struct ata_timing timing;
  245. int ret;
  246. /* Compute ATA timing and set it to SMC */
  247. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  248. if (ret) {
  249. dev_warn(ap->dev, "Failed to compute ATA timing %d, "
  250. "set PIO_0 timing\n", ret);
  251. timing = *ata_timing_find_mode(XFER_PIO_0);
  252. }
  253. set_smc_timing(ap->dev, adev, info, &timing);
  254. }
  255. static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
  256. unsigned char *buf, unsigned int buflen, int rw)
  257. {
  258. struct at91_ide_info *info = dev->link->ap->host->private_data;
  259. unsigned int consumed;
  260. unsigned int mode;
  261. unsigned long flags;
  262. local_irq_save(flags);
  263. regmap_fields_read(fields.mode, info->cs, &mode);
  264. /* set 16bit mode before writing data */
  265. regmap_fields_write(fields.mode, info->cs, (mode & ~AT91_SMC_DBW) |
  266. AT91_SMC_DBW_16);
  267. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  268. /* restore 8bit mode after data is written */
  269. regmap_fields_write(fields.mode, info->cs, (mode & ~AT91_SMC_DBW) |
  270. AT91_SMC_DBW_8);
  271. local_irq_restore(flags);
  272. return consumed;
  273. }
  274. static struct scsi_host_template pata_at91_sht = {
  275. ATA_PIO_SHT(DRV_NAME),
  276. };
  277. static struct ata_port_operations pata_at91_port_ops = {
  278. .inherits = &ata_sff_port_ops,
  279. .sff_data_xfer = pata_at91_data_xfer_noirq,
  280. .set_piomode = pata_at91_set_piomode,
  281. .cable_detect = ata_cable_40wire,
  282. };
  283. static int at91sam9_smc_fields_init(struct device *dev)
  284. {
  285. struct reg_field field = REG_FIELD(0, 0, 31);
  286. field.id_size = 8;
  287. field.id_offset = AT91SAM9_SMC_GENERIC_BLK_SZ;
  288. field.reg = AT91SAM9_SMC_SETUP(AT91SAM9_SMC_GENERIC);
  289. fields.setup = devm_regmap_field_alloc(dev, smc, field);
  290. if (IS_ERR(fields.setup))
  291. return PTR_ERR(fields.setup);
  292. field.reg = AT91SAM9_SMC_PULSE(AT91SAM9_SMC_GENERIC);
  293. fields.pulse = devm_regmap_field_alloc(dev, smc, field);
  294. if (IS_ERR(fields.pulse))
  295. return PTR_ERR(fields.pulse);
  296. field.reg = AT91SAM9_SMC_CYCLE(AT91SAM9_SMC_GENERIC);
  297. fields.cycle = devm_regmap_field_alloc(dev, smc, field);
  298. if (IS_ERR(fields.cycle))
  299. return PTR_ERR(fields.cycle);
  300. field.reg = AT91SAM9_SMC_MODE(AT91SAM9_SMC_GENERIC);
  301. fields.mode = devm_regmap_field_alloc(dev, smc, field);
  302. return PTR_ERR_OR_ZERO(fields.mode);
  303. }
  304. static int pata_at91_probe(struct platform_device *pdev)
  305. {
  306. struct at91_cf_data *board = dev_get_platdata(&pdev->dev);
  307. struct device *dev = &pdev->dev;
  308. struct at91_ide_info *info;
  309. struct resource *mem_res;
  310. struct ata_host *host;
  311. struct ata_port *ap;
  312. int irq_flags = 0;
  313. int irq = 0;
  314. int ret;
  315. /* get platform resources: IO/CTL memories and irq/rst pins */
  316. if (pdev->num_resources != 1) {
  317. dev_err(&pdev->dev, "invalid number of resources\n");
  318. return -EINVAL;
  319. }
  320. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. if (!mem_res) {
  322. dev_err(dev, "failed to get mem resource\n");
  323. return -EINVAL;
  324. }
  325. irq = board->irq_pin;
  326. smc = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "atmel,smc");
  327. if (IS_ERR(smc))
  328. return PTR_ERR(smc);
  329. ret = at91sam9_smc_fields_init(dev);
  330. if (ret < 0)
  331. return ret;
  332. /* init ata host */
  333. host = ata_host_alloc(dev, 1);
  334. if (!host)
  335. return -ENOMEM;
  336. ap = host->ports[0];
  337. ap->ops = &pata_at91_port_ops;
  338. ap->flags |= ATA_FLAG_SLAVE_POSS;
  339. ap->pio_mask = ATA_PIO4;
  340. if (!gpio_is_valid(irq)) {
  341. ap->flags |= ATA_FLAG_PIO_POLLING;
  342. ata_port_desc(ap, "no IRQ, using PIO polling");
  343. }
  344. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  345. if (!info) {
  346. dev_err(dev, "failed to allocate memory for private data\n");
  347. return -ENOMEM;
  348. }
  349. info->mck = clk_get(NULL, "mck");
  350. if (IS_ERR(info->mck)) {
  351. dev_err(dev, "failed to get access to mck clock\n");
  352. return -ENODEV;
  353. }
  354. info->cs = board->chipselect;
  355. info->mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  356. AT91_SMC_EXNWMODE_READY | AT91_SMC_BAT_SELECT |
  357. AT91_SMC_DBW_8 | AT91_SMC_TDF_(0);
  358. info->ide_addr = devm_ioremap(dev,
  359. mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
  360. if (!info->ide_addr) {
  361. dev_err(dev, "failed to map IO base\n");
  362. ret = -ENOMEM;
  363. goto err_put;
  364. }
  365. info->alt_addr = devm_ioremap(dev,
  366. mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
  367. if (!info->alt_addr) {
  368. dev_err(dev, "failed to map CTL base\n");
  369. ret = -ENOMEM;
  370. goto err_put;
  371. }
  372. ap->ioaddr.cmd_addr = info->ide_addr;
  373. ap->ioaddr.ctl_addr = info->alt_addr + 0x06;
  374. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  375. ata_sff_std_ports(&ap->ioaddr);
  376. ata_port_desc(ap, "mmio cmd 0x%llx ctl 0x%llx",
  377. (unsigned long long)mem_res->start + CF_IDE_OFFSET,
  378. (unsigned long long)mem_res->start + CF_ALT_IDE_OFFSET);
  379. host->private_data = info;
  380. ret = ata_host_activate(host, gpio_is_valid(irq) ? gpio_to_irq(irq) : 0,
  381. gpio_is_valid(irq) ? ata_sff_interrupt : NULL,
  382. irq_flags, &pata_at91_sht);
  383. if (ret)
  384. goto err_put;
  385. return 0;
  386. err_put:
  387. clk_put(info->mck);
  388. return ret;
  389. }
  390. static int pata_at91_remove(struct platform_device *pdev)
  391. {
  392. struct ata_host *host = platform_get_drvdata(pdev);
  393. struct at91_ide_info *info;
  394. if (!host)
  395. return 0;
  396. info = host->private_data;
  397. ata_host_detach(host);
  398. if (!info)
  399. return 0;
  400. clk_put(info->mck);
  401. return 0;
  402. }
  403. static struct platform_driver pata_at91_driver = {
  404. .probe = pata_at91_probe,
  405. .remove = pata_at91_remove,
  406. .driver = {
  407. .name = DRV_NAME,
  408. },
  409. };
  410. module_platform_driver(pata_at91_driver);
  411. MODULE_LICENSE("GPL");
  412. MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
  413. MODULE_AUTHOR("Matyukevich Sergey");
  414. MODULE_VERSION(DRV_VERSION);