spi-sh-hspi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * SuperH HSPI bus driver
  3. *
  4. * Copyright (C) 2011 Kuninori Morimoto
  5. *
  6. * Based on spi-sh.c:
  7. * Based on pxa2xx_spi.c:
  8. * Copyright (C) 2011 Renesas Solutions Corp.
  9. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. #include <linux/clk.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/timer.h>
  29. #include <linux/delay.h>
  30. #include <linux/list.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/io.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/sh_hspi.h>
  37. #define SPCR 0x00
  38. #define SPSR 0x04
  39. #define SPSCR 0x08
  40. #define SPTBR 0x0C
  41. #define SPRBR 0x10
  42. #define SPCR2 0x14
  43. /* SPSR */
  44. #define RXFL (1 << 2)
  45. #define hspi2info(h) (h->dev->platform_data)
  46. struct hspi_priv {
  47. void __iomem *addr;
  48. struct spi_master *master;
  49. struct device *dev;
  50. struct clk *clk;
  51. };
  52. /*
  53. * basic function
  54. */
  55. static void hspi_write(struct hspi_priv *hspi, int reg, u32 val)
  56. {
  57. iowrite32(val, hspi->addr + reg);
  58. }
  59. static u32 hspi_read(struct hspi_priv *hspi, int reg)
  60. {
  61. return ioread32(hspi->addr + reg);
  62. }
  63. /*
  64. * transfer function
  65. */
  66. static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
  67. {
  68. int t = 256;
  69. while (t--) {
  70. if ((mask & hspi_read(hspi, SPSR)) == val)
  71. return 0;
  72. msleep(20);
  73. }
  74. dev_err(hspi->dev, "timeout\n");
  75. return -ETIMEDOUT;
  76. }
  77. /*
  78. * spi master function
  79. */
  80. static int hspi_prepare_transfer(struct spi_master *master)
  81. {
  82. struct hspi_priv *hspi = spi_master_get_devdata(master);
  83. pm_runtime_get_sync(hspi->dev);
  84. return 0;
  85. }
  86. static int hspi_unprepare_transfer(struct spi_master *master)
  87. {
  88. struct hspi_priv *hspi = spi_master_get_devdata(master);
  89. pm_runtime_put_sync(hspi->dev);
  90. return 0;
  91. }
  92. static void hspi_hw_setup(struct hspi_priv *hspi,
  93. struct spi_message *msg,
  94. struct spi_transfer *t)
  95. {
  96. struct spi_device *spi = msg->spi;
  97. struct device *dev = hspi->dev;
  98. u32 target_rate;
  99. u32 spcr, idiv_clk;
  100. u32 rate, best_rate, min, tmp;
  101. target_rate = t ? t->speed_hz : 0;
  102. if (!target_rate)
  103. target_rate = spi->max_speed_hz;
  104. /*
  105. * find best IDIV/CLKCx settings
  106. */
  107. min = ~0;
  108. best_rate = 0;
  109. spcr = 0;
  110. for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
  111. rate = clk_get_rate(hspi->clk);
  112. /* IDIV calculation */
  113. if (idiv_clk & (1 << 5))
  114. rate /= 128;
  115. else
  116. rate /= 16;
  117. /* CLKCx calculation */
  118. rate /= (((idiv_clk & 0x1F) + 1) * 2) ;
  119. /* save best settings */
  120. tmp = abs(target_rate - rate);
  121. if (tmp < min) {
  122. min = tmp;
  123. spcr = idiv_clk;
  124. best_rate = rate;
  125. }
  126. }
  127. if (spi->mode & SPI_CPHA)
  128. spcr |= 1 << 7;
  129. if (spi->mode & SPI_CPOL)
  130. spcr |= 1 << 6;
  131. dev_dbg(dev, "speed %d/%d\n", target_rate, best_rate);
  132. hspi_write(hspi, SPCR, spcr);
  133. hspi_write(hspi, SPSR, 0x0);
  134. hspi_write(hspi, SPSCR, 0x1); /* master mode */
  135. }
  136. static int hspi_transfer_one_message(struct spi_master *master,
  137. struct spi_message *msg)
  138. {
  139. struct hspi_priv *hspi = spi_master_get_devdata(master);
  140. struct spi_transfer *t;
  141. u32 tx;
  142. u32 rx;
  143. int ret, i;
  144. dev_dbg(hspi->dev, "%s\n", __func__);
  145. ret = 0;
  146. list_for_each_entry(t, &msg->transfers, transfer_list) {
  147. hspi_hw_setup(hspi, msg, t);
  148. for (i = 0; i < t->len; i++) {
  149. /* wait remains */
  150. ret = hspi_status_check_timeout(hspi, 0x1, 0);
  151. if (ret < 0)
  152. break;
  153. tx = 0;
  154. if (t->tx_buf)
  155. tx = (u32)((u8 *)t->tx_buf)[i];
  156. hspi_write(hspi, SPTBR, tx);
  157. /* wait recive */
  158. ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
  159. if (ret < 0)
  160. break;
  161. rx = hspi_read(hspi, SPRBR);
  162. if (t->rx_buf)
  163. ((u8 *)t->rx_buf)[i] = (u8)rx;
  164. }
  165. msg->actual_length += t->len;
  166. }
  167. msg->status = ret;
  168. spi_finalize_current_message(master);
  169. return ret;
  170. }
  171. static int hspi_setup(struct spi_device *spi)
  172. {
  173. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  174. struct device *dev = hspi->dev;
  175. if (8 != spi->bits_per_word) {
  176. dev_err(dev, "bits_per_word should be 8\n");
  177. return -EIO;
  178. }
  179. dev_dbg(dev, "%s setup\n", spi->modalias);
  180. return 0;
  181. }
  182. static void hspi_cleanup(struct spi_device *spi)
  183. {
  184. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  185. struct device *dev = hspi->dev;
  186. dev_dbg(dev, "%s cleanup\n", spi->modalias);
  187. }
  188. static int __devinit hspi_probe(struct platform_device *pdev)
  189. {
  190. struct resource *res;
  191. struct spi_master *master;
  192. struct hspi_priv *hspi;
  193. struct clk *clk;
  194. int ret;
  195. /* get base addr */
  196. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  197. if (!res) {
  198. dev_err(&pdev->dev, "invalid resource\n");
  199. return -EINVAL;
  200. }
  201. master = spi_alloc_master(&pdev->dev, sizeof(*hspi));
  202. if (!master) {
  203. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  204. return -ENOMEM;
  205. }
  206. clk = clk_get(NULL, "shyway_clk");
  207. if (!clk) {
  208. dev_err(&pdev->dev, "shyway_clk is required\n");
  209. ret = -EINVAL;
  210. goto error0;
  211. }
  212. hspi = spi_master_get_devdata(master);
  213. dev_set_drvdata(&pdev->dev, hspi);
  214. /* init hspi */
  215. hspi->master = master;
  216. hspi->dev = &pdev->dev;
  217. hspi->clk = clk;
  218. hspi->addr = devm_ioremap(hspi->dev,
  219. res->start, resource_size(res));
  220. if (!hspi->addr) {
  221. dev_err(&pdev->dev, "ioremap error.\n");
  222. ret = -ENOMEM;
  223. goto error1;
  224. }
  225. master->num_chipselect = 1;
  226. master->bus_num = pdev->id;
  227. master->setup = hspi_setup;
  228. master->cleanup = hspi_cleanup;
  229. master->mode_bits = SPI_CPOL | SPI_CPHA;
  230. master->prepare_transfer_hardware = hspi_prepare_transfer;
  231. master->transfer_one_message = hspi_transfer_one_message;
  232. master->unprepare_transfer_hardware = hspi_unprepare_transfer;
  233. ret = spi_register_master(master);
  234. if (ret < 0) {
  235. dev_err(&pdev->dev, "spi_register_master error.\n");
  236. goto error2;
  237. }
  238. pm_runtime_enable(&pdev->dev);
  239. dev_info(&pdev->dev, "probed\n");
  240. return 0;
  241. error2:
  242. devm_iounmap(hspi->dev, hspi->addr);
  243. error1:
  244. clk_put(clk);
  245. error0:
  246. spi_master_put(master);
  247. return ret;
  248. }
  249. static int __devexit hspi_remove(struct platform_device *pdev)
  250. {
  251. struct hspi_priv *hspi = dev_get_drvdata(&pdev->dev);
  252. pm_runtime_disable(&pdev->dev);
  253. clk_put(hspi->clk);
  254. spi_unregister_master(hspi->master);
  255. devm_iounmap(hspi->dev, hspi->addr);
  256. return 0;
  257. }
  258. static struct platform_driver hspi_driver = {
  259. .probe = hspi_probe,
  260. .remove = __devexit_p(hspi_remove),
  261. .driver = {
  262. .name = "sh-hspi",
  263. .owner = THIS_MODULE,
  264. },
  265. };
  266. module_platform_driver(hspi_driver);
  267. MODULE_DESCRIPTION("SuperH HSPI bus driver");
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  270. MODULE_ALIAS("platform:sh_spi");