spi-sh-hspi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set)
  64. {
  65. u32 val = hspi_read(hspi, reg);
  66. val &= ~mask;
  67. val |= set & mask;
  68. hspi_write(hspi, reg, val);
  69. }
  70. /*
  71. * transfer function
  72. */
  73. static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
  74. {
  75. int t = 256;
  76. while (t--) {
  77. if ((mask & hspi_read(hspi, SPSR)) == val)
  78. return 0;
  79. msleep(20);
  80. }
  81. dev_err(hspi->dev, "timeout\n");
  82. return -ETIMEDOUT;
  83. }
  84. /*
  85. * spi master function
  86. */
  87. static int hspi_prepare_transfer(struct spi_master *master)
  88. {
  89. struct hspi_priv *hspi = spi_master_get_devdata(master);
  90. pm_runtime_get_sync(hspi->dev);
  91. return 0;
  92. }
  93. static int hspi_unprepare_transfer(struct spi_master *master)
  94. {
  95. struct hspi_priv *hspi = spi_master_get_devdata(master);
  96. pm_runtime_put_sync(hspi->dev);
  97. return 0;
  98. }
  99. #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0)
  100. #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1)
  101. static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi)
  102. {
  103. hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6);
  104. }
  105. static void hspi_hw_setup(struct hspi_priv *hspi,
  106. struct spi_message *msg,
  107. struct spi_transfer *t)
  108. {
  109. struct spi_device *spi = msg->spi;
  110. struct device *dev = hspi->dev;
  111. u32 target_rate;
  112. u32 spcr, idiv_clk;
  113. u32 rate, best_rate, min, tmp;
  114. target_rate = t ? t->speed_hz : 0;
  115. if (!target_rate)
  116. target_rate = spi->max_speed_hz;
  117. /*
  118. * find best IDIV/CLKCx settings
  119. */
  120. min = ~0;
  121. best_rate = 0;
  122. spcr = 0;
  123. for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
  124. rate = clk_get_rate(hspi->clk);
  125. /* IDIV calculation */
  126. if (idiv_clk & (1 << 5))
  127. rate /= 128;
  128. else
  129. rate /= 16;
  130. /* CLKCx calculation */
  131. rate /= (((idiv_clk & 0x1F) + 1) * 2) ;
  132. /* save best settings */
  133. tmp = abs(target_rate - rate);
  134. if (tmp < min) {
  135. min = tmp;
  136. spcr = idiv_clk;
  137. best_rate = rate;
  138. }
  139. }
  140. if (spi->mode & SPI_CPHA)
  141. spcr |= 1 << 7;
  142. if (spi->mode & SPI_CPOL)
  143. spcr |= 1 << 6;
  144. dev_dbg(dev, "speed %d/%d\n", target_rate, best_rate);
  145. hspi_write(hspi, SPCR, spcr);
  146. hspi_write(hspi, SPSR, 0x0);
  147. hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */
  148. }
  149. static int hspi_transfer_one_message(struct spi_master *master,
  150. struct spi_message *msg)
  151. {
  152. struct hspi_priv *hspi = spi_master_get_devdata(master);
  153. struct spi_transfer *t;
  154. u32 tx;
  155. u32 rx;
  156. int ret, i;
  157. unsigned int cs_change;
  158. const int nsecs = 50;
  159. dev_dbg(hspi->dev, "%s\n", __func__);
  160. cs_change = 1;
  161. ret = 0;
  162. list_for_each_entry(t, &msg->transfers, transfer_list) {
  163. if (cs_change) {
  164. hspi_hw_setup(hspi, msg, t);
  165. hspi_hw_cs_enable(hspi);
  166. ndelay(nsecs);
  167. }
  168. cs_change = t->cs_change;
  169. for (i = 0; i < t->len; i++) {
  170. /* wait remains */
  171. ret = hspi_status_check_timeout(hspi, 0x1, 0);
  172. if (ret < 0)
  173. break;
  174. tx = 0;
  175. if (t->tx_buf)
  176. tx = (u32)((u8 *)t->tx_buf)[i];
  177. hspi_write(hspi, SPTBR, tx);
  178. /* wait recive */
  179. ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
  180. if (ret < 0)
  181. break;
  182. rx = hspi_read(hspi, SPRBR);
  183. if (t->rx_buf)
  184. ((u8 *)t->rx_buf)[i] = (u8)rx;
  185. }
  186. msg->actual_length += t->len;
  187. if (t->delay_usecs)
  188. udelay(t->delay_usecs);
  189. if (cs_change) {
  190. ndelay(nsecs);
  191. hspi_hw_cs_disable(hspi);
  192. ndelay(nsecs);
  193. }
  194. }
  195. msg->status = ret;
  196. if (!cs_change) {
  197. ndelay(nsecs);
  198. hspi_hw_cs_disable(hspi);
  199. }
  200. spi_finalize_current_message(master);
  201. return ret;
  202. }
  203. static int hspi_setup(struct spi_device *spi)
  204. {
  205. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  206. struct device *dev = hspi->dev;
  207. if (8 != spi->bits_per_word) {
  208. dev_err(dev, "bits_per_word should be 8\n");
  209. return -EIO;
  210. }
  211. dev_dbg(dev, "%s setup\n", spi->modalias);
  212. return 0;
  213. }
  214. static void hspi_cleanup(struct spi_device *spi)
  215. {
  216. struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
  217. struct device *dev = hspi->dev;
  218. dev_dbg(dev, "%s cleanup\n", spi->modalias);
  219. }
  220. static int hspi_probe(struct platform_device *pdev)
  221. {
  222. struct resource *res;
  223. struct spi_master *master;
  224. struct hspi_priv *hspi;
  225. struct clk *clk;
  226. int ret;
  227. /* get base addr */
  228. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  229. if (!res) {
  230. dev_err(&pdev->dev, "invalid resource\n");
  231. return -EINVAL;
  232. }
  233. master = spi_alloc_master(&pdev->dev, sizeof(*hspi));
  234. if (!master) {
  235. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  236. return -ENOMEM;
  237. }
  238. clk = clk_get(NULL, "shyway_clk");
  239. if (IS_ERR(clk)) {
  240. dev_err(&pdev->dev, "shyway_clk is required\n");
  241. ret = -EINVAL;
  242. goto error0;
  243. }
  244. hspi = spi_master_get_devdata(master);
  245. platform_set_drvdata(pdev, hspi);
  246. /* init hspi */
  247. hspi->master = master;
  248. hspi->dev = &pdev->dev;
  249. hspi->clk = clk;
  250. hspi->addr = devm_ioremap(hspi->dev,
  251. res->start, resource_size(res));
  252. if (!hspi->addr) {
  253. dev_err(&pdev->dev, "ioremap error.\n");
  254. ret = -ENOMEM;
  255. goto error1;
  256. }
  257. master->num_chipselect = 1;
  258. master->bus_num = pdev->id;
  259. master->setup = hspi_setup;
  260. master->cleanup = hspi_cleanup;
  261. master->mode_bits = SPI_CPOL | SPI_CPHA;
  262. master->prepare_transfer_hardware = hspi_prepare_transfer;
  263. master->transfer_one_message = hspi_transfer_one_message;
  264. master->unprepare_transfer_hardware = hspi_unprepare_transfer;
  265. ret = spi_register_master(master);
  266. if (ret < 0) {
  267. dev_err(&pdev->dev, "spi_register_master error.\n");
  268. goto error1;
  269. }
  270. pm_runtime_enable(&pdev->dev);
  271. dev_info(&pdev->dev, "probed\n");
  272. return 0;
  273. error1:
  274. clk_put(clk);
  275. error0:
  276. spi_master_put(master);
  277. return ret;
  278. }
  279. static int hspi_remove(struct platform_device *pdev)
  280. {
  281. struct hspi_priv *hspi = platform_get_drvdata(pdev);
  282. pm_runtime_disable(&pdev->dev);
  283. clk_put(hspi->clk);
  284. spi_unregister_master(hspi->master);
  285. return 0;
  286. }
  287. static struct platform_driver hspi_driver = {
  288. .probe = hspi_probe,
  289. .remove = hspi_remove,
  290. .driver = {
  291. .name = "sh-hspi",
  292. .owner = THIS_MODULE,
  293. },
  294. };
  295. module_platform_driver(hspi_driver);
  296. MODULE_DESCRIPTION("SuperH HSPI bus driver");
  297. MODULE_LICENSE("GPL");
  298. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  299. MODULE_ALIAS("platform:sh_spi");