pata_mpc52xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * drivers/ata/pata_mpc52xx.c
  3. *
  4. * libata driver for the Freescale MPC52xx on-chip IDE interface
  5. *
  6. * Copyright (C) 2006 Sylvain Munaut <tnt@246tNt.com>
  7. * Copyright (C) 2003 Mipsys - Benjamin Herrenschmidt
  8. *
  9. * This file is licensed under the terms of the GNU General Public License
  10. * version 2. This program is licensed "as is" without any warranty of any
  11. * kind, whether express or implied.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/libata.h>
  18. #include <asm/io.h>
  19. #include <asm/types.h>
  20. #include <asm/prom.h>
  21. #include <asm/of_platform.h>
  22. #include <asm/mpc52xx.h>
  23. #define DRV_NAME "mpc52xx_ata"
  24. #define DRV_VERSION "0.1.0"
  25. /* Private structures used by the driver */
  26. struct mpc52xx_ata_timings {
  27. u32 pio1;
  28. u32 pio2;
  29. };
  30. struct mpc52xx_ata_priv {
  31. unsigned int ipb_period;
  32. struct mpc52xx_ata __iomem * ata_regs;
  33. int ata_irq;
  34. struct mpc52xx_ata_timings timings[2];
  35. int csel;
  36. };
  37. /* ATAPI-4 PIO specs (in ns) */
  38. static const int ataspec_t0[5] = {600, 383, 240, 180, 120};
  39. static const int ataspec_t1[5] = { 70, 50, 30, 30, 25};
  40. static const int ataspec_t2_8[5] = {290, 290, 290, 80, 70};
  41. static const int ataspec_t2_16[5] = {165, 125, 100, 80, 70};
  42. static const int ataspec_t2i[5] = { 0, 0, 0, 70, 25};
  43. static const int ataspec_t4[5] = { 30, 20, 15, 10, 10};
  44. static const int ataspec_ta[5] = { 35, 35, 35, 35, 35};
  45. #define CALC_CLKCYC(c,v) ((((v)+(c)-1)/(c)))
  46. /* Bit definitions inside the registers */
  47. #define MPC52xx_ATA_HOSTCONF_SMR 0x80000000UL /* State machine reset */
  48. #define MPC52xx_ATA_HOSTCONF_FR 0x40000000UL /* FIFO Reset */
  49. #define MPC52xx_ATA_HOSTCONF_IE 0x02000000UL /* Enable interrupt in PIO */
  50. #define MPC52xx_ATA_HOSTCONF_IORDY 0x01000000UL /* Drive supports IORDY protocol */
  51. #define MPC52xx_ATA_HOSTSTAT_TIP 0x80000000UL /* Transaction in progress */
  52. #define MPC52xx_ATA_HOSTSTAT_UREP 0x40000000UL /* UDMA Read Extended Pause */
  53. #define MPC52xx_ATA_HOSTSTAT_RERR 0x02000000UL /* Read Error */
  54. #define MPC52xx_ATA_HOSTSTAT_WERR 0x01000000UL /* Write Error */
  55. #define MPC52xx_ATA_FIFOSTAT_EMPTY 0x01 /* FIFO Empty */
  56. #define MPC52xx_ATA_DMAMODE_WRITE 0x01 /* Write DMA */
  57. #define MPC52xx_ATA_DMAMODE_READ 0x02 /* Read DMA */
  58. #define MPC52xx_ATA_DMAMODE_UDMA 0x04 /* UDMA enabled */
  59. #define MPC52xx_ATA_DMAMODE_IE 0x08 /* Enable drive interrupt to CPU in DMA mode */
  60. #define MPC52xx_ATA_DMAMODE_FE 0x10 /* FIFO Flush enable in Rx mode */
  61. #define MPC52xx_ATA_DMAMODE_FR 0x20 /* FIFO Reset */
  62. #define MPC52xx_ATA_DMAMODE_HUT 0x40 /* Host UDMA burst terminate */
  63. /* Structure of the hardware registers */
  64. struct mpc52xx_ata {
  65. /* Host interface registers */
  66. u32 config; /* ATA + 0x00 Host configuration */
  67. u32 host_status; /* ATA + 0x04 Host controller status */
  68. u32 pio1; /* ATA + 0x08 PIO Timing 1 */
  69. u32 pio2; /* ATA + 0x0c PIO Timing 2 */
  70. u32 mdma1; /* ATA + 0x10 MDMA Timing 1 */
  71. u32 mdma2; /* ATA + 0x14 MDMA Timing 2 */
  72. u32 udma1; /* ATA + 0x18 UDMA Timing 1 */
  73. u32 udma2; /* ATA + 0x1c UDMA Timing 2 */
  74. u32 udma3; /* ATA + 0x20 UDMA Timing 3 */
  75. u32 udma4; /* ATA + 0x24 UDMA Timing 4 */
  76. u32 udma5; /* ATA + 0x28 UDMA Timing 5 */
  77. u32 share_cnt; /* ATA + 0x2c ATA share counter */
  78. u32 reserved0[3];
  79. /* FIFO registers */
  80. u32 fifo_data; /* ATA + 0x3c */
  81. u8 fifo_status_frame; /* ATA + 0x40 */
  82. u8 fifo_status; /* ATA + 0x41 */
  83. u16 reserved7[1];
  84. u8 fifo_control; /* ATA + 0x44 */
  85. u8 reserved8[5];
  86. u16 fifo_alarm; /* ATA + 0x4a */
  87. u16 reserved9;
  88. u16 fifo_rdp; /* ATA + 0x4e */
  89. u16 reserved10;
  90. u16 fifo_wrp; /* ATA + 0x52 */
  91. u16 reserved11;
  92. u16 fifo_lfrdp; /* ATA + 0x56 */
  93. u16 reserved12;
  94. u16 fifo_lfwrp; /* ATA + 0x5a */
  95. /* Drive TaskFile registers */
  96. u8 tf_control; /* ATA + 0x5c TASKFILE Control/Alt Status */
  97. u8 reserved13[3];
  98. u16 tf_data; /* ATA + 0x60 TASKFILE Data */
  99. u16 reserved14;
  100. u8 tf_features; /* ATA + 0x64 TASKFILE Features/Error */
  101. u8 reserved15[3];
  102. u8 tf_sec_count; /* ATA + 0x68 TASKFILE Sector Count */
  103. u8 reserved16[3];
  104. u8 tf_sec_num; /* ATA + 0x6c TASKFILE Sector Number */
  105. u8 reserved17[3];
  106. u8 tf_cyl_low; /* ATA + 0x70 TASKFILE Cylinder Low */
  107. u8 reserved18[3];
  108. u8 tf_cyl_high; /* ATA + 0x74 TASKFILE Cylinder High */
  109. u8 reserved19[3];
  110. u8 tf_dev_head; /* ATA + 0x78 TASKFILE Device/Head */
  111. u8 reserved20[3];
  112. u8 tf_command; /* ATA + 0x7c TASKFILE Command/Status */
  113. u8 dma_mode; /* ATA + 0x7d ATA Host DMA Mode configuration */
  114. u8 reserved21[2];
  115. };
  116. /* ======================================================================== */
  117. /* Aux fns */
  118. /* ======================================================================== */
  119. /* MPC52xx low level hw control */
  120. static int
  121. mpc52xx_ata_compute_pio_timings(struct mpc52xx_ata_priv *priv, int dev, int pio)
  122. {
  123. struct mpc52xx_ata_timings *timing = &priv->timings[dev];
  124. unsigned int ipb_period = priv->ipb_period;
  125. unsigned int t0, t1, t2_8, t2_16, t2i, t4, ta;
  126. if ((pio<0) || (pio>4))
  127. return -EINVAL;
  128. t0 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t0[pio]);
  129. t1 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t1[pio]);
  130. t2_8 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_8[pio]);
  131. t2_16 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_16[pio]);
  132. t2i = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2i[pio]);
  133. t4 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t4[pio]);
  134. ta = CALC_CLKCYC(ipb_period, 1000 * ataspec_ta[pio]);
  135. timing->pio1 = (t0 << 24) | (t2_8 << 16) | (t2_16 << 8) | (t2i);
  136. timing->pio2 = (t4 << 24) | (t1 << 16) | (ta << 8);
  137. return 0;
  138. }
  139. static void
  140. mpc52xx_ata_apply_timings(struct mpc52xx_ata_priv *priv, int device)
  141. {
  142. struct mpc52xx_ata __iomem *regs = priv->ata_regs;
  143. struct mpc52xx_ata_timings *timing = &priv->timings[device];
  144. out_be32(&regs->pio1, timing->pio1);
  145. out_be32(&regs->pio2, timing->pio2);
  146. out_be32(&regs->mdma1, 0);
  147. out_be32(&regs->mdma2, 0);
  148. out_be32(&regs->udma1, 0);
  149. out_be32(&regs->udma2, 0);
  150. out_be32(&regs->udma3, 0);
  151. out_be32(&regs->udma4, 0);
  152. out_be32(&regs->udma5, 0);
  153. priv->csel = device;
  154. }
  155. static int
  156. mpc52xx_ata_hw_init(struct mpc52xx_ata_priv *priv)
  157. {
  158. struct mpc52xx_ata __iomem *regs = priv->ata_regs;
  159. int tslot;
  160. /* Clear share_cnt (all sample code do this ...) */
  161. out_be32(&regs->share_cnt, 0);
  162. /* Configure and reset host */
  163. out_be32(&regs->config,
  164. MPC52xx_ATA_HOSTCONF_IE |
  165. MPC52xx_ATA_HOSTCONF_IORDY |
  166. MPC52xx_ATA_HOSTCONF_SMR |
  167. MPC52xx_ATA_HOSTCONF_FR);
  168. udelay(10);
  169. out_be32(&regs->config,
  170. MPC52xx_ATA_HOSTCONF_IE |
  171. MPC52xx_ATA_HOSTCONF_IORDY);
  172. /* Set the time slot to 1us */
  173. tslot = CALC_CLKCYC(priv->ipb_period, 1000000);
  174. out_be32(&regs->share_cnt, tslot << 16 );
  175. /* Init timings to PIO0 */
  176. memset(priv->timings, 0x00, 2*sizeof(struct mpc52xx_ata_timings));
  177. mpc52xx_ata_compute_pio_timings(priv, 0, 0);
  178. mpc52xx_ata_compute_pio_timings(priv, 1, 0);
  179. mpc52xx_ata_apply_timings(priv, 0);
  180. return 0;
  181. }
  182. /* ======================================================================== */
  183. /* libata driver */
  184. /* ======================================================================== */
  185. static void
  186. mpc52xx_ata_set_piomode(struct ata_port *ap, struct ata_device *adev)
  187. {
  188. struct mpc52xx_ata_priv *priv = ap->host->private_data;
  189. int pio, rv;
  190. pio = adev->pio_mode - XFER_PIO_0;
  191. rv = mpc52xx_ata_compute_pio_timings(priv, adev->devno, pio);
  192. if (rv) {
  193. printk(KERN_ERR DRV_NAME
  194. ": Trying to select invalid PIO mode %d\n", pio);
  195. return;
  196. }
  197. mpc52xx_ata_apply_timings(priv, adev->devno);
  198. }
  199. static void
  200. mpc52xx_ata_dev_select(struct ata_port *ap, unsigned int device)
  201. {
  202. struct mpc52xx_ata_priv *priv = ap->host->private_data;
  203. if (device != priv->csel)
  204. mpc52xx_ata_apply_timings(priv, device);
  205. ata_std_dev_select(ap,device);
  206. }
  207. static void
  208. mpc52xx_ata_error_handler(struct ata_port *ap)
  209. {
  210. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, NULL,
  211. ata_std_postreset);
  212. }
  213. static struct scsi_host_template mpc52xx_ata_sht = {
  214. .module = THIS_MODULE,
  215. .name = DRV_NAME,
  216. .ioctl = ata_scsi_ioctl,
  217. .queuecommand = ata_scsi_queuecmd,
  218. .can_queue = ATA_DEF_QUEUE,
  219. .this_id = ATA_SHT_THIS_ID,
  220. .sg_tablesize = LIBATA_MAX_PRD,
  221. .max_sectors = ATA_MAX_SECTORS,
  222. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  223. .emulated = ATA_SHT_EMULATED,
  224. .use_clustering = ATA_SHT_USE_CLUSTERING,
  225. .proc_name = DRV_NAME,
  226. .dma_boundary = ATA_DMA_BOUNDARY,
  227. .slave_configure = ata_scsi_slave_config,
  228. .bios_param = ata_std_bios_param,
  229. };
  230. static struct ata_port_operations mpc52xx_ata_port_ops = {
  231. .port_disable = ata_port_disable,
  232. .set_piomode = mpc52xx_ata_set_piomode,
  233. .dev_select = mpc52xx_ata_dev_select,
  234. .tf_load = ata_tf_load,
  235. .tf_read = ata_tf_read,
  236. .check_status = ata_check_status,
  237. .exec_command = ata_exec_command,
  238. .freeze = ata_bmdma_freeze,
  239. .thaw = ata_bmdma_thaw,
  240. .error_handler = mpc52xx_ata_error_handler,
  241. .qc_prep = ata_qc_prep,
  242. .qc_issue = ata_qc_issue_prot,
  243. .data_xfer = ata_mmio_data_xfer,
  244. .irq_handler = ata_interrupt,
  245. .irq_clear = ata_bmdma_irq_clear,
  246. .port_start = ata_port_start,
  247. .port_stop = ata_port_stop,
  248. .host_stop = ata_host_stop,
  249. };
  250. static struct ata_probe_ent mpc52xx_ata_probe_ent = {
  251. .port_ops = &mpc52xx_ata_port_ops,
  252. .sht = &mpc52xx_ata_sht,
  253. .n_ports = 1,
  254. .pio_mask = 0x1f, /* Up to PIO4 */
  255. .mwdma_mask = 0x00, /* No MWDMA */
  256. .udma_mask = 0x00, /* No UDMA */
  257. .port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST | ATA_FLAG_MMIO,
  258. .irq_flags = 0,
  259. };
  260. static int __devinit
  261. mpc52xx_ata_init_one(struct device *dev, struct mpc52xx_ata_priv *priv)
  262. {
  263. struct ata_probe_ent *ae = &mpc52xx_ata_probe_ent;
  264. struct ata_ioports *aio = &ae->port[0];
  265. int rv;
  266. INIT_LIST_HEAD(&ae->node);
  267. ae->dev = dev;
  268. ae->irq = priv->ata_irq;
  269. aio->cmd_addr = 0; /* Don't have a classic reg block */
  270. aio->altstatus_addr = (unsigned long)&priv->ata_regs->tf_control;
  271. aio->ctl_addr = (unsigned long)&priv->ata_regs->tf_control;
  272. aio->data_addr = (unsigned long)&priv->ata_regs->tf_data;
  273. aio->error_addr = (unsigned long)&priv->ata_regs->tf_features;
  274. aio->feature_addr = (unsigned long)&priv->ata_regs->tf_features;
  275. aio->nsect_addr = (unsigned long)&priv->ata_regs->tf_sec_count;
  276. aio->lbal_addr = (unsigned long)&priv->ata_regs->tf_sec_num;
  277. aio->lbam_addr = (unsigned long)&priv->ata_regs->tf_cyl_low;
  278. aio->lbah_addr = (unsigned long)&priv->ata_regs->tf_cyl_high;
  279. aio->device_addr = (unsigned long)&priv->ata_regs->tf_dev_head;
  280. aio->status_addr = (unsigned long)&priv->ata_regs->tf_command;
  281. aio->command_addr = (unsigned long)&priv->ata_regs->tf_command;
  282. ae->private_data = priv;
  283. rv = ata_device_add(ae);
  284. return rv ? 0 : -EINVAL;
  285. }
  286. static struct mpc52xx_ata_priv *
  287. mpc52xx_ata_remove_one(struct device *dev)
  288. {
  289. struct ata_host *host = dev_get_drvdata(dev);
  290. struct mpc52xx_ata_priv *priv = host->private_data;
  291. ata_host_remove(host);
  292. return priv;
  293. }
  294. /* ======================================================================== */
  295. /* OF Platform driver */
  296. /* ======================================================================== */
  297. static int __devinit
  298. mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
  299. {
  300. unsigned int ipb_freq;
  301. struct resource res_mem;
  302. int ata_irq = NO_IRQ;
  303. struct mpc52xx_ata __iomem *ata_regs = NULL;
  304. struct mpc52xx_ata_priv *priv = NULL;
  305. int rv;
  306. /* Get ipb frequency */
  307. ipb_freq = mpc52xx_find_ipb_freq(op->node);
  308. if (!ipb_freq) {
  309. printk(KERN_ERR DRV_NAME ": "
  310. "Unable to find IPB Bus frequency\n" );
  311. return -ENODEV;
  312. }
  313. /* Get IRQ and register */
  314. rv = of_address_to_resource(op->node, 0, &res_mem);
  315. if (rv) {
  316. printk(KERN_ERR DRV_NAME ": "
  317. "Error while parsing device node resource\n" );
  318. return rv;
  319. }
  320. ata_irq = irq_of_parse_and_map(op->node, 0);
  321. if (ata_irq == NO_IRQ) {
  322. printk(KERN_ERR DRV_NAME ": "
  323. "Error while mapping the irq\n");
  324. return -EINVAL;
  325. }
  326. /* Request mem region */
  327. if (!request_mem_region(res_mem.start,
  328. sizeof(struct mpc52xx_ata), DRV_NAME)) {
  329. printk(KERN_ERR DRV_NAME ": "
  330. "Error while requesting mem region\n");
  331. irq_dispose_mapping(ata_irq);
  332. return -EBUSY;
  333. }
  334. /* Remap registers */
  335. ata_regs = ioremap(res_mem.start, sizeof(struct mpc52xx_ata));
  336. if (!ata_regs) {
  337. printk(KERN_ERR DRV_NAME ": "
  338. "Error while mapping register set\n");
  339. rv = -ENOMEM;
  340. goto err;
  341. }
  342. /* Prepare our private structure */
  343. priv = kmalloc(sizeof(struct mpc52xx_ata_priv), GFP_ATOMIC);
  344. if (!priv) {
  345. printk(KERN_ERR DRV_NAME ": "
  346. "Error while allocating private structure\n");
  347. rv = -ENOMEM;
  348. goto err;
  349. }
  350. priv->ipb_period = 1000000000 / (ipb_freq / 1000);
  351. priv->ata_regs = ata_regs;
  352. priv->ata_irq = ata_irq;
  353. priv->csel = -1;
  354. /* Init the hw */
  355. rv = mpc52xx_ata_hw_init(priv);
  356. if (rv) {
  357. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  358. goto err;
  359. }
  360. /* Register ourselves to libata */
  361. rv = mpc52xx_ata_init_one(&op->dev, priv);
  362. if (rv) {
  363. printk(KERN_ERR DRV_NAME ": "
  364. "Error while registering to ATA layer\n");
  365. return rv;
  366. }
  367. /* Done */
  368. return 0;
  369. /* Error path */
  370. err:
  371. kfree(priv);
  372. if (ata_regs)
  373. iounmap(ata_regs);
  374. release_mem_region(res_mem.start, sizeof(struct mpc52xx_ata));
  375. irq_dispose_mapping(ata_irq);
  376. return rv;
  377. }
  378. static int
  379. mpc52xx_ata_remove(struct of_device *op)
  380. {
  381. struct mpc52xx_ata_priv *priv;
  382. struct resource res_mem;
  383. int rv;
  384. /* Unregister */
  385. priv = mpc52xx_ata_remove_one(&op->dev);
  386. /* Free everything */
  387. iounmap(priv->ata_regs);
  388. rv = of_address_to_resource(op->node, 0, &res_mem);
  389. if (rv) {
  390. printk(KERN_ERR DRV_NAME ": "
  391. "Error while parsing device node resource\n");
  392. printk(KERN_ERR DRV_NAME ": "
  393. "Zone may not be properly released\n");
  394. } else
  395. release_mem_region(res_mem.start, sizeof(struct mpc52xx_ata));
  396. irq_dispose_mapping(priv->ata_irq);
  397. kfree(priv);
  398. return 0;
  399. }
  400. #ifdef CONFIG_PM
  401. static int
  402. mpc52xx_ata_suspend(struct of_device *op, pm_message_t state)
  403. {
  404. return 0; /* FIXME : What to do here ? */
  405. }
  406. static int
  407. mpc52xx_ata_resume(struct of_device *op)
  408. {
  409. return 0; /* FIXME : What to do here ? */
  410. }
  411. #endif
  412. static struct of_device_id mpc52xx_ata_of_match[] = {
  413. {
  414. .compatible = "mpc5200-ata",
  415. },
  416. {
  417. .compatible = "mpc52xx-ata",
  418. },
  419. {},
  420. };
  421. static struct of_platform_driver mpc52xx_ata_of_platform_driver = {
  422. .owner = THIS_MODULE,
  423. .name = DRV_NAME,
  424. .match_table = mpc52xx_ata_of_match,
  425. .probe = mpc52xx_ata_probe,
  426. .remove = mpc52xx_ata_remove,
  427. #ifdef CONFIG_PM
  428. .suspend = mpc52xx_ata_suspend,
  429. .resume = mpc52xx_ata_resume,
  430. #endif
  431. .driver = {
  432. .name = DRV_NAME,
  433. .owner = THIS_MODULE,
  434. },
  435. };
  436. /* ======================================================================== */
  437. /* Module */
  438. /* ======================================================================== */
  439. static int __init
  440. mpc52xx_ata_init(void)
  441. {
  442. printk(KERN_INFO "ata: MPC52xx IDE/ATA libata driver\n");
  443. return of_register_platform_driver(&mpc52xx_ata_of_platform_driver);
  444. }
  445. static void __exit
  446. mpc52xx_ata_exit(void)
  447. {
  448. of_unregister_platform_driver(&mpc52xx_ata_of_platform_driver);
  449. }
  450. module_init(mpc52xx_ata_init);
  451. module_exit(mpc52xx_ata_exit);
  452. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  453. MODULE_DESCRIPTION("Freescale MPC52xx IDE/ATA libata driver");
  454. MODULE_LICENSE("GPL");
  455. MODULE_DEVICE_TABLE(of, mpc52xx_ata_of_match);
  456. MODULE_VERSION(DRV_VERSION);