spi-omap2-mcspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/omap-dma.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/spi-omap2-mcspi.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  40. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  41. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  42. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  51. /* per-channel banks, 0x14 bytes each, first is: */
  52. #define OMAP2_MCSPI_CHCONF0 0x2c
  53. #define OMAP2_MCSPI_CHSTAT0 0x30
  54. #define OMAP2_MCSPI_CHCTRL0 0x34
  55. #define OMAP2_MCSPI_TX0 0x38
  56. #define OMAP2_MCSPI_RX0 0x3c
  57. /* per-register bitmasks: */
  58. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  78. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  79. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  84. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  85. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  86. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  87. /* We have 2 DMA channels per CS, one for RX and one for TX */
  88. struct omap2_mcspi_dma {
  89. struct dma_chan *dma_tx;
  90. struct dma_chan *dma_rx;
  91. int dma_tx_sync_dev;
  92. int dma_rx_sync_dev;
  93. struct completion dma_tx_completion;
  94. struct completion dma_rx_completion;
  95. char dma_rx_ch_name[14];
  96. char dma_tx_ch_name[14];
  97. };
  98. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  99. * cache operations; better heuristics consider wordsize and bitrate.
  100. */
  101. #define DMA_MIN_BYTES 160
  102. /*
  103. * Used for context save and restore, structure members to be updated whenever
  104. * corresponding registers are modified.
  105. */
  106. struct omap2_mcspi_regs {
  107. u32 modulctrl;
  108. u32 wakeupenable;
  109. struct list_head cs;
  110. };
  111. struct omap2_mcspi {
  112. struct spi_master *master;
  113. /* Virtual base address of the controller */
  114. void __iomem *base;
  115. unsigned long phys;
  116. /* SPI1 has 4 channels, while SPI2 has 2 */
  117. struct omap2_mcspi_dma *dma_channels;
  118. struct device *dev;
  119. struct omap2_mcspi_regs ctx;
  120. int fifo_depth;
  121. unsigned int pin_dir:1;
  122. };
  123. struct omap2_mcspi_cs {
  124. void __iomem *base;
  125. unsigned long phys;
  126. int word_len;
  127. u16 mode;
  128. struct list_head node;
  129. /* Context save and restore shadow register */
  130. u32 chconf0, chctrl0;
  131. };
  132. static inline void mcspi_write_reg(struct spi_master *master,
  133. int idx, u32 val)
  134. {
  135. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  136. writel_relaxed(val, mcspi->base + idx);
  137. }
  138. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. return readl_relaxed(mcspi->base + idx);
  142. }
  143. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  144. int idx, u32 val)
  145. {
  146. struct omap2_mcspi_cs *cs = spi->controller_state;
  147. writel_relaxed(val, cs->base + idx);
  148. }
  149. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. return readl_relaxed(cs->base + idx);
  153. }
  154. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return cs->chconf0;
  158. }
  159. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. cs->chconf0 = val;
  163. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  164. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  165. }
  166. static inline int mcspi_bytes_per_word(int word_len)
  167. {
  168. if (word_len <= 8)
  169. return 1;
  170. else if (word_len <= 16)
  171. return 2;
  172. else /* word_len <= 32 */
  173. return 4;
  174. }
  175. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  176. int is_read, int enable)
  177. {
  178. u32 l, rw;
  179. l = mcspi_cached_chconf0(spi);
  180. if (is_read) /* 1 is read, 0 write */
  181. rw = OMAP2_MCSPI_CHCONF_DMAR;
  182. else
  183. rw = OMAP2_MCSPI_CHCONF_DMAW;
  184. if (enable)
  185. l |= rw;
  186. else
  187. l &= ~rw;
  188. mcspi_write_chconf0(spi, l);
  189. }
  190. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  191. {
  192. struct omap2_mcspi_cs *cs = spi->controller_state;
  193. u32 l;
  194. l = cs->chctrl0;
  195. if (enable)
  196. l |= OMAP2_MCSPI_CHCTRL_EN;
  197. else
  198. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  199. cs->chctrl0 = l;
  200. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  201. /* Flash post-writes */
  202. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  203. }
  204. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  205. {
  206. u32 l;
  207. l = mcspi_cached_chconf0(spi);
  208. if (cs_active)
  209. l |= OMAP2_MCSPI_CHCONF_FORCE;
  210. else
  211. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  212. mcspi_write_chconf0(spi, l);
  213. }
  214. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  215. {
  216. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  217. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  218. u32 l;
  219. /*
  220. * Setup when switching from (reset default) slave mode
  221. * to single-channel master mode
  222. */
  223. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  224. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  225. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  226. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  227. ctx->modulctrl = l;
  228. }
  229. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  230. struct spi_transfer *t, int enable)
  231. {
  232. struct spi_master *master = spi->master;
  233. struct omap2_mcspi_cs *cs = spi->controller_state;
  234. struct omap2_mcspi *mcspi;
  235. unsigned int wcnt;
  236. int max_fifo_depth, fifo_depth, bytes_per_word;
  237. u32 chconf, xferlevel;
  238. mcspi = spi_master_get_devdata(master);
  239. chconf = mcspi_cached_chconf0(spi);
  240. if (enable) {
  241. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  242. if (t->len % bytes_per_word != 0)
  243. goto disable_fifo;
  244. if (t->rx_buf != NULL && t->tx_buf != NULL)
  245. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  246. else
  247. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  248. fifo_depth = gcd(t->len, max_fifo_depth);
  249. if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
  250. goto disable_fifo;
  251. wcnt = t->len / bytes_per_word;
  252. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  253. goto disable_fifo;
  254. xferlevel = wcnt << 16;
  255. if (t->rx_buf != NULL) {
  256. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  257. xferlevel |= (fifo_depth - 1) << 8;
  258. }
  259. if (t->tx_buf != NULL) {
  260. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  261. xferlevel |= fifo_depth - 1;
  262. }
  263. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  264. mcspi_write_chconf0(spi, chconf);
  265. mcspi->fifo_depth = fifo_depth;
  266. return;
  267. }
  268. disable_fifo:
  269. if (t->rx_buf != NULL)
  270. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  271. if (t->tx_buf != NULL)
  272. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  273. mcspi_write_chconf0(spi, chconf);
  274. mcspi->fifo_depth = 0;
  275. }
  276. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  277. {
  278. struct spi_master *spi_cntrl = mcspi->master;
  279. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  280. struct omap2_mcspi_cs *cs;
  281. /* McSPI: context restore */
  282. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  283. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  284. list_for_each_entry(cs, &ctx->cs, node)
  285. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  286. }
  287. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  288. {
  289. unsigned long timeout;
  290. timeout = jiffies + msecs_to_jiffies(1000);
  291. while (!(readl_relaxed(reg) & bit)) {
  292. if (time_after(jiffies, timeout)) {
  293. if (!(readl_relaxed(reg) & bit))
  294. return -ETIMEDOUT;
  295. else
  296. return 0;
  297. }
  298. cpu_relax();
  299. }
  300. return 0;
  301. }
  302. static void omap2_mcspi_rx_callback(void *data)
  303. {
  304. struct spi_device *spi = data;
  305. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  306. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  307. /* We must disable the DMA RX request */
  308. omap2_mcspi_set_dma_req(spi, 1, 0);
  309. complete(&mcspi_dma->dma_rx_completion);
  310. }
  311. static void omap2_mcspi_tx_callback(void *data)
  312. {
  313. struct spi_device *spi = data;
  314. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  315. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  316. /* We must disable the DMA TX request */
  317. omap2_mcspi_set_dma_req(spi, 0, 0);
  318. complete(&mcspi_dma->dma_tx_completion);
  319. }
  320. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  321. struct spi_transfer *xfer,
  322. struct dma_slave_config cfg)
  323. {
  324. struct omap2_mcspi *mcspi;
  325. struct omap2_mcspi_dma *mcspi_dma;
  326. unsigned int count;
  327. mcspi = spi_master_get_devdata(spi->master);
  328. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  329. count = xfer->len;
  330. if (mcspi_dma->dma_tx) {
  331. struct dma_async_tx_descriptor *tx;
  332. struct scatterlist sg;
  333. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  334. sg_init_table(&sg, 1);
  335. sg_dma_address(&sg) = xfer->tx_dma;
  336. sg_dma_len(&sg) = xfer->len;
  337. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
  338. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  339. if (tx) {
  340. tx->callback = omap2_mcspi_tx_callback;
  341. tx->callback_param = spi;
  342. dmaengine_submit(tx);
  343. } else {
  344. /* FIXME: fall back to PIO? */
  345. }
  346. }
  347. dma_async_issue_pending(mcspi_dma->dma_tx);
  348. omap2_mcspi_set_dma_req(spi, 0, 1);
  349. }
  350. static unsigned
  351. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  352. struct dma_slave_config cfg,
  353. unsigned es)
  354. {
  355. struct omap2_mcspi *mcspi;
  356. struct omap2_mcspi_dma *mcspi_dma;
  357. unsigned int count, dma_count;
  358. u32 l;
  359. int elements = 0;
  360. int word_len, element_count;
  361. struct omap2_mcspi_cs *cs = spi->controller_state;
  362. mcspi = spi_master_get_devdata(spi->master);
  363. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  364. count = xfer->len;
  365. dma_count = xfer->len;
  366. if (mcspi->fifo_depth == 0)
  367. dma_count -= es;
  368. word_len = cs->word_len;
  369. l = mcspi_cached_chconf0(spi);
  370. if (word_len <= 8)
  371. element_count = count;
  372. else if (word_len <= 16)
  373. element_count = count >> 1;
  374. else /* word_len <= 32 */
  375. element_count = count >> 2;
  376. if (mcspi_dma->dma_rx) {
  377. struct dma_async_tx_descriptor *tx;
  378. struct scatterlist sg;
  379. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  380. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  381. dma_count -= es;
  382. sg_init_table(&sg, 1);
  383. sg_dma_address(&sg) = xfer->rx_dma;
  384. sg_dma_len(&sg) = dma_count;
  385. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
  386. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
  387. DMA_CTRL_ACK);
  388. if (tx) {
  389. tx->callback = omap2_mcspi_rx_callback;
  390. tx->callback_param = spi;
  391. dmaengine_submit(tx);
  392. } else {
  393. /* FIXME: fall back to PIO? */
  394. }
  395. }
  396. dma_async_issue_pending(mcspi_dma->dma_rx);
  397. omap2_mcspi_set_dma_req(spi, 1, 1);
  398. wait_for_completion(&mcspi_dma->dma_rx_completion);
  399. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  400. DMA_FROM_DEVICE);
  401. if (mcspi->fifo_depth > 0)
  402. return count;
  403. omap2_mcspi_set_enable(spi, 0);
  404. elements = element_count - 1;
  405. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  406. elements--;
  407. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  408. & OMAP2_MCSPI_CHSTAT_RXS)) {
  409. u32 w;
  410. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  411. if (word_len <= 8)
  412. ((u8 *)xfer->rx_buf)[elements++] = w;
  413. else if (word_len <= 16)
  414. ((u16 *)xfer->rx_buf)[elements++] = w;
  415. else /* word_len <= 32 */
  416. ((u32 *)xfer->rx_buf)[elements++] = w;
  417. } else {
  418. int bytes_per_word = mcspi_bytes_per_word(word_len);
  419. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  420. count -= (bytes_per_word << 1);
  421. omap2_mcspi_set_enable(spi, 1);
  422. return count;
  423. }
  424. }
  425. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  426. & OMAP2_MCSPI_CHSTAT_RXS)) {
  427. u32 w;
  428. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  429. if (word_len <= 8)
  430. ((u8 *)xfer->rx_buf)[elements] = w;
  431. else if (word_len <= 16)
  432. ((u16 *)xfer->rx_buf)[elements] = w;
  433. else /* word_len <= 32 */
  434. ((u32 *)xfer->rx_buf)[elements] = w;
  435. } else {
  436. dev_err(&spi->dev, "DMA RX last word empty\n");
  437. count -= mcspi_bytes_per_word(word_len);
  438. }
  439. omap2_mcspi_set_enable(spi, 1);
  440. return count;
  441. }
  442. static unsigned
  443. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  444. {
  445. struct omap2_mcspi *mcspi;
  446. struct omap2_mcspi_cs *cs = spi->controller_state;
  447. struct omap2_mcspi_dma *mcspi_dma;
  448. unsigned int count;
  449. u32 l;
  450. u8 *rx;
  451. const u8 *tx;
  452. struct dma_slave_config cfg;
  453. enum dma_slave_buswidth width;
  454. unsigned es;
  455. u32 burst;
  456. void __iomem *chstat_reg;
  457. void __iomem *irqstat_reg;
  458. int wait_res;
  459. mcspi = spi_master_get_devdata(spi->master);
  460. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  461. l = mcspi_cached_chconf0(spi);
  462. if (cs->word_len <= 8) {
  463. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  464. es = 1;
  465. } else if (cs->word_len <= 16) {
  466. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  467. es = 2;
  468. } else {
  469. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  470. es = 4;
  471. }
  472. count = xfer->len;
  473. burst = 1;
  474. if (mcspi->fifo_depth > 0) {
  475. if (count > mcspi->fifo_depth)
  476. burst = mcspi->fifo_depth / es;
  477. else
  478. burst = count / es;
  479. }
  480. memset(&cfg, 0, sizeof(cfg));
  481. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  482. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  483. cfg.src_addr_width = width;
  484. cfg.dst_addr_width = width;
  485. cfg.src_maxburst = burst;
  486. cfg.dst_maxburst = burst;
  487. rx = xfer->rx_buf;
  488. tx = xfer->tx_buf;
  489. if (tx != NULL)
  490. omap2_mcspi_tx_dma(spi, xfer, cfg);
  491. if (rx != NULL)
  492. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  493. if (tx != NULL) {
  494. wait_for_completion(&mcspi_dma->dma_tx_completion);
  495. dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
  496. DMA_TO_DEVICE);
  497. if (mcspi->fifo_depth > 0) {
  498. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  499. if (mcspi_wait_for_reg_bit(irqstat_reg,
  500. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  501. dev_err(&spi->dev, "EOW timed out\n");
  502. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  503. OMAP2_MCSPI_IRQSTATUS_EOW);
  504. }
  505. /* for TX_ONLY mode, be sure all words have shifted out */
  506. if (rx == NULL) {
  507. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  508. if (mcspi->fifo_depth > 0) {
  509. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  510. OMAP2_MCSPI_CHSTAT_TXFFE);
  511. if (wait_res < 0)
  512. dev_err(&spi->dev, "TXFFE timed out\n");
  513. } else {
  514. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  515. OMAP2_MCSPI_CHSTAT_TXS);
  516. if (wait_res < 0)
  517. dev_err(&spi->dev, "TXS timed out\n");
  518. }
  519. if (wait_res >= 0 &&
  520. (mcspi_wait_for_reg_bit(chstat_reg,
  521. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  522. dev_err(&spi->dev, "EOT timed out\n");
  523. }
  524. }
  525. return count;
  526. }
  527. static unsigned
  528. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  529. {
  530. struct omap2_mcspi *mcspi;
  531. struct omap2_mcspi_cs *cs = spi->controller_state;
  532. unsigned int count, c;
  533. u32 l;
  534. void __iomem *base = cs->base;
  535. void __iomem *tx_reg;
  536. void __iomem *rx_reg;
  537. void __iomem *chstat_reg;
  538. int word_len;
  539. mcspi = spi_master_get_devdata(spi->master);
  540. count = xfer->len;
  541. c = count;
  542. word_len = cs->word_len;
  543. l = mcspi_cached_chconf0(spi);
  544. /* We store the pre-calculated register addresses on stack to speed
  545. * up the transfer loop. */
  546. tx_reg = base + OMAP2_MCSPI_TX0;
  547. rx_reg = base + OMAP2_MCSPI_RX0;
  548. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  549. if (c < (word_len>>3))
  550. return 0;
  551. if (word_len <= 8) {
  552. u8 *rx;
  553. const u8 *tx;
  554. rx = xfer->rx_buf;
  555. tx = xfer->tx_buf;
  556. do {
  557. c -= 1;
  558. if (tx != NULL) {
  559. if (mcspi_wait_for_reg_bit(chstat_reg,
  560. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  561. dev_err(&spi->dev, "TXS timed out\n");
  562. goto out;
  563. }
  564. dev_vdbg(&spi->dev, "write-%d %02x\n",
  565. word_len, *tx);
  566. writel_relaxed(*tx++, tx_reg);
  567. }
  568. if (rx != NULL) {
  569. if (mcspi_wait_for_reg_bit(chstat_reg,
  570. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  571. dev_err(&spi->dev, "RXS timed out\n");
  572. goto out;
  573. }
  574. if (c == 1 && tx == NULL &&
  575. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  576. omap2_mcspi_set_enable(spi, 0);
  577. *rx++ = readl_relaxed(rx_reg);
  578. dev_vdbg(&spi->dev, "read-%d %02x\n",
  579. word_len, *(rx - 1));
  580. if (mcspi_wait_for_reg_bit(chstat_reg,
  581. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  582. dev_err(&spi->dev,
  583. "RXS timed out\n");
  584. goto out;
  585. }
  586. c = 0;
  587. } else if (c == 0 && tx == NULL) {
  588. omap2_mcspi_set_enable(spi, 0);
  589. }
  590. *rx++ = readl_relaxed(rx_reg);
  591. dev_vdbg(&spi->dev, "read-%d %02x\n",
  592. word_len, *(rx - 1));
  593. }
  594. } while (c);
  595. } else if (word_len <= 16) {
  596. u16 *rx;
  597. const u16 *tx;
  598. rx = xfer->rx_buf;
  599. tx = xfer->tx_buf;
  600. do {
  601. c -= 2;
  602. if (tx != NULL) {
  603. if (mcspi_wait_for_reg_bit(chstat_reg,
  604. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  605. dev_err(&spi->dev, "TXS timed out\n");
  606. goto out;
  607. }
  608. dev_vdbg(&spi->dev, "write-%d %04x\n",
  609. word_len, *tx);
  610. writel_relaxed(*tx++, tx_reg);
  611. }
  612. if (rx != NULL) {
  613. if (mcspi_wait_for_reg_bit(chstat_reg,
  614. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  615. dev_err(&spi->dev, "RXS timed out\n");
  616. goto out;
  617. }
  618. if (c == 2 && tx == NULL &&
  619. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  620. omap2_mcspi_set_enable(spi, 0);
  621. *rx++ = readl_relaxed(rx_reg);
  622. dev_vdbg(&spi->dev, "read-%d %04x\n",
  623. word_len, *(rx - 1));
  624. if (mcspi_wait_for_reg_bit(chstat_reg,
  625. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  626. dev_err(&spi->dev,
  627. "RXS timed out\n");
  628. goto out;
  629. }
  630. c = 0;
  631. } else if (c == 0 && tx == NULL) {
  632. omap2_mcspi_set_enable(spi, 0);
  633. }
  634. *rx++ = readl_relaxed(rx_reg);
  635. dev_vdbg(&spi->dev, "read-%d %04x\n",
  636. word_len, *(rx - 1));
  637. }
  638. } while (c >= 2);
  639. } else if (word_len <= 32) {
  640. u32 *rx;
  641. const u32 *tx;
  642. rx = xfer->rx_buf;
  643. tx = xfer->tx_buf;
  644. do {
  645. c -= 4;
  646. if (tx != NULL) {
  647. if (mcspi_wait_for_reg_bit(chstat_reg,
  648. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  649. dev_err(&spi->dev, "TXS timed out\n");
  650. goto out;
  651. }
  652. dev_vdbg(&spi->dev, "write-%d %08x\n",
  653. word_len, *tx);
  654. writel_relaxed(*tx++, tx_reg);
  655. }
  656. if (rx != NULL) {
  657. if (mcspi_wait_for_reg_bit(chstat_reg,
  658. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  659. dev_err(&spi->dev, "RXS timed out\n");
  660. goto out;
  661. }
  662. if (c == 4 && tx == NULL &&
  663. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  664. omap2_mcspi_set_enable(spi, 0);
  665. *rx++ = readl_relaxed(rx_reg);
  666. dev_vdbg(&spi->dev, "read-%d %08x\n",
  667. word_len, *(rx - 1));
  668. if (mcspi_wait_for_reg_bit(chstat_reg,
  669. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  670. dev_err(&spi->dev,
  671. "RXS timed out\n");
  672. goto out;
  673. }
  674. c = 0;
  675. } else if (c == 0 && tx == NULL) {
  676. omap2_mcspi_set_enable(spi, 0);
  677. }
  678. *rx++ = readl_relaxed(rx_reg);
  679. dev_vdbg(&spi->dev, "read-%d %08x\n",
  680. word_len, *(rx - 1));
  681. }
  682. } while (c >= 4);
  683. }
  684. /* for TX_ONLY mode, be sure all words have shifted out */
  685. if (xfer->rx_buf == NULL) {
  686. if (mcspi_wait_for_reg_bit(chstat_reg,
  687. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  688. dev_err(&spi->dev, "TXS timed out\n");
  689. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  690. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  691. dev_err(&spi->dev, "EOT timed out\n");
  692. /* disable chan to purge rx datas received in TX_ONLY transfer,
  693. * otherwise these rx datas will affect the direct following
  694. * RX_ONLY transfer.
  695. */
  696. omap2_mcspi_set_enable(spi, 0);
  697. }
  698. out:
  699. omap2_mcspi_set_enable(spi, 1);
  700. return count - c;
  701. }
  702. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  703. {
  704. u32 div;
  705. for (div = 0; div < 15; div++)
  706. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  707. return div;
  708. return 15;
  709. }
  710. /* called only when no transfer is active to this device */
  711. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  712. struct spi_transfer *t)
  713. {
  714. struct omap2_mcspi_cs *cs = spi->controller_state;
  715. struct omap2_mcspi *mcspi;
  716. struct spi_master *spi_cntrl;
  717. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  718. u8 word_len = spi->bits_per_word;
  719. u32 speed_hz = spi->max_speed_hz;
  720. mcspi = spi_master_get_devdata(spi->master);
  721. spi_cntrl = mcspi->master;
  722. if (t != NULL && t->bits_per_word)
  723. word_len = t->bits_per_word;
  724. cs->word_len = word_len;
  725. if (t && t->speed_hz)
  726. speed_hz = t->speed_hz;
  727. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  728. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  729. clkd = omap2_mcspi_calc_divisor(speed_hz);
  730. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  731. clkg = 0;
  732. } else {
  733. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  734. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  735. clkd = (div - 1) & 0xf;
  736. extclk = (div - 1) >> 4;
  737. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  738. }
  739. l = mcspi_cached_chconf0(spi);
  740. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  741. * REVISIT: this controller could support SPI_3WIRE mode.
  742. */
  743. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  744. l &= ~OMAP2_MCSPI_CHCONF_IS;
  745. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  746. l |= OMAP2_MCSPI_CHCONF_DPE0;
  747. } else {
  748. l |= OMAP2_MCSPI_CHCONF_IS;
  749. l |= OMAP2_MCSPI_CHCONF_DPE1;
  750. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  751. }
  752. /* wordlength */
  753. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  754. l |= (word_len - 1) << 7;
  755. /* set chipselect polarity; manage with FORCE */
  756. if (!(spi->mode & SPI_CS_HIGH))
  757. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  758. else
  759. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  760. /* set clock divisor */
  761. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  762. l |= clkd << 2;
  763. /* set clock granularity */
  764. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  765. l |= clkg;
  766. if (clkg) {
  767. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  768. cs->chctrl0 |= extclk << 8;
  769. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  770. }
  771. /* set SPI mode 0..3 */
  772. if (spi->mode & SPI_CPOL)
  773. l |= OMAP2_MCSPI_CHCONF_POL;
  774. else
  775. l &= ~OMAP2_MCSPI_CHCONF_POL;
  776. if (spi->mode & SPI_CPHA)
  777. l |= OMAP2_MCSPI_CHCONF_PHA;
  778. else
  779. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  780. mcspi_write_chconf0(spi, l);
  781. cs->mode = spi->mode;
  782. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  783. speed_hz,
  784. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  785. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  786. return 0;
  787. }
  788. /*
  789. * Note that we currently allow DMA only if we get a channel
  790. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  791. */
  792. static int omap2_mcspi_request_dma(struct spi_device *spi)
  793. {
  794. struct spi_master *master = spi->master;
  795. struct omap2_mcspi *mcspi;
  796. struct omap2_mcspi_dma *mcspi_dma;
  797. dma_cap_mask_t mask;
  798. unsigned sig;
  799. mcspi = spi_master_get_devdata(master);
  800. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  801. init_completion(&mcspi_dma->dma_rx_completion);
  802. init_completion(&mcspi_dma->dma_tx_completion);
  803. dma_cap_zero(mask);
  804. dma_cap_set(DMA_SLAVE, mask);
  805. sig = mcspi_dma->dma_rx_sync_dev;
  806. mcspi_dma->dma_rx =
  807. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  808. &sig, &master->dev,
  809. mcspi_dma->dma_rx_ch_name);
  810. if (!mcspi_dma->dma_rx)
  811. goto no_dma;
  812. sig = mcspi_dma->dma_tx_sync_dev;
  813. mcspi_dma->dma_tx =
  814. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  815. &sig, &master->dev,
  816. mcspi_dma->dma_tx_ch_name);
  817. if (!mcspi_dma->dma_tx) {
  818. dma_release_channel(mcspi_dma->dma_rx);
  819. mcspi_dma->dma_rx = NULL;
  820. goto no_dma;
  821. }
  822. return 0;
  823. no_dma:
  824. dev_warn(&spi->dev, "not using DMA for McSPI\n");
  825. return -EAGAIN;
  826. }
  827. static int omap2_mcspi_setup(struct spi_device *spi)
  828. {
  829. int ret;
  830. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  831. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  832. struct omap2_mcspi_dma *mcspi_dma;
  833. struct omap2_mcspi_cs *cs = spi->controller_state;
  834. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  835. if (!cs) {
  836. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  837. if (!cs)
  838. return -ENOMEM;
  839. cs->base = mcspi->base + spi->chip_select * 0x14;
  840. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  841. cs->mode = 0;
  842. cs->chconf0 = 0;
  843. cs->chctrl0 = 0;
  844. spi->controller_state = cs;
  845. /* Link this to context save list */
  846. list_add_tail(&cs->node, &ctx->cs);
  847. }
  848. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  849. ret = omap2_mcspi_request_dma(spi);
  850. if (ret < 0 && ret != -EAGAIN)
  851. return ret;
  852. }
  853. if (gpio_is_valid(spi->cs_gpio)) {
  854. if (gpio_request(spi->cs_gpio, dev_name(&spi->dev)) == 0)
  855. gpio_direction_output(spi->cs_gpio,
  856. !(spi->mode & SPI_CS_HIGH));
  857. }
  858. ret = pm_runtime_get_sync(mcspi->dev);
  859. if (ret < 0)
  860. return ret;
  861. ret = omap2_mcspi_setup_transfer(spi, NULL);
  862. pm_runtime_mark_last_busy(mcspi->dev);
  863. pm_runtime_put_autosuspend(mcspi->dev);
  864. return ret;
  865. }
  866. static void omap2_mcspi_cleanup(struct spi_device *spi)
  867. {
  868. struct omap2_mcspi *mcspi;
  869. struct omap2_mcspi_dma *mcspi_dma;
  870. struct omap2_mcspi_cs *cs;
  871. mcspi = spi_master_get_devdata(spi->master);
  872. if (spi->controller_state) {
  873. /* Unlink controller state from context save list */
  874. cs = spi->controller_state;
  875. list_del(&cs->node);
  876. kfree(cs);
  877. }
  878. if (spi->chip_select < spi->master->num_chipselect) {
  879. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  880. if (mcspi_dma->dma_rx) {
  881. dma_release_channel(mcspi_dma->dma_rx);
  882. mcspi_dma->dma_rx = NULL;
  883. }
  884. if (mcspi_dma->dma_tx) {
  885. dma_release_channel(mcspi_dma->dma_tx);
  886. mcspi_dma->dma_tx = NULL;
  887. }
  888. }
  889. if (gpio_is_valid(spi->cs_gpio))
  890. gpio_free(spi->cs_gpio);
  891. }
  892. static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
  893. struct spi_device *spi, struct spi_transfer *t)
  894. {
  895. /* We only enable one channel at a time -- the one whose message is
  896. * -- although this controller would gladly
  897. * arbitrate among multiple channels. This corresponds to "single
  898. * channel" master mode. As a side effect, we need to manage the
  899. * chipselect with the FORCE bit ... CS != channel enable.
  900. */
  901. struct spi_master *master;
  902. struct omap2_mcspi_dma *mcspi_dma;
  903. int cs_active = 0;
  904. struct omap2_mcspi_cs *cs;
  905. struct omap2_mcspi_device_config *cd;
  906. int par_override = 0;
  907. int status = 0;
  908. u32 chconf;
  909. master = spi->master;
  910. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  911. cs = spi->controller_state;
  912. cd = spi->controller_data;
  913. /*
  914. * The slave driver could have changed spi->mode in which case
  915. * it will be different from cs->mode (the current hardware setup).
  916. * If so, set par_override (even though its not a parity issue) so
  917. * omap2_mcspi_setup_transfer will be called to configure the hardware
  918. * with the correct mode on the first iteration of the loop below.
  919. */
  920. if (spi->mode != cs->mode)
  921. par_override = 1;
  922. omap2_mcspi_set_enable(spi, 0);
  923. if (par_override ||
  924. (t->speed_hz != spi->max_speed_hz) ||
  925. (t->bits_per_word != spi->bits_per_word)) {
  926. par_override = 1;
  927. status = omap2_mcspi_setup_transfer(spi, t);
  928. if (status < 0)
  929. goto out;
  930. if (t->speed_hz == spi->max_speed_hz &&
  931. t->bits_per_word == spi->bits_per_word)
  932. par_override = 0;
  933. }
  934. if (cd && cd->cs_per_word) {
  935. chconf = mcspi->ctx.modulctrl;
  936. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  937. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  938. mcspi->ctx.modulctrl =
  939. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  940. }
  941. if (!cs_active) {
  942. omap2_mcspi_force_cs(spi, 1);
  943. cs_active = 1;
  944. }
  945. chconf = mcspi_cached_chconf0(spi);
  946. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  947. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  948. if (t->tx_buf == NULL)
  949. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  950. else if (t->rx_buf == NULL)
  951. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  952. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  953. /* Turbo mode is for more than one word */
  954. if (t->len > ((cs->word_len + 7) >> 3))
  955. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  956. }
  957. mcspi_write_chconf0(spi, chconf);
  958. if (t->len) {
  959. unsigned count;
  960. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  961. (t->len >= DMA_MIN_BYTES))
  962. omap2_mcspi_set_fifo(spi, t, 1);
  963. omap2_mcspi_set_enable(spi, 1);
  964. /* RX_ONLY mode needs dummy data in TX reg */
  965. if (t->tx_buf == NULL)
  966. writel_relaxed(0, cs->base
  967. + OMAP2_MCSPI_TX0);
  968. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  969. (t->len >= DMA_MIN_BYTES))
  970. count = omap2_mcspi_txrx_dma(spi, t);
  971. else
  972. count = omap2_mcspi_txrx_pio(spi, t);
  973. if (count != t->len) {
  974. status = -EIO;
  975. goto out;
  976. }
  977. }
  978. if (t->delay_usecs)
  979. udelay(t->delay_usecs);
  980. /* ignore the "leave it on after last xfer" hint */
  981. if (t->cs_change) {
  982. omap2_mcspi_force_cs(spi, 0);
  983. cs_active = 0;
  984. }
  985. omap2_mcspi_set_enable(spi, 0);
  986. if (mcspi->fifo_depth > 0)
  987. omap2_mcspi_set_fifo(spi, t, 0);
  988. out:
  989. /* Restore defaults if they were overriden */
  990. if (par_override) {
  991. par_override = 0;
  992. status = omap2_mcspi_setup_transfer(spi, NULL);
  993. }
  994. if (cs_active)
  995. omap2_mcspi_force_cs(spi, 0);
  996. if (cd && cd->cs_per_word) {
  997. chconf = mcspi->ctx.modulctrl;
  998. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  999. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1000. mcspi->ctx.modulctrl =
  1001. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1002. }
  1003. omap2_mcspi_set_enable(spi, 0);
  1004. if (mcspi->fifo_depth > 0 && t)
  1005. omap2_mcspi_set_fifo(spi, t, 0);
  1006. return status;
  1007. }
  1008. static int omap2_mcspi_transfer_one(struct spi_master *master,
  1009. struct spi_device *spi, struct spi_transfer *t)
  1010. {
  1011. struct omap2_mcspi *mcspi;
  1012. struct omap2_mcspi_dma *mcspi_dma;
  1013. const void *tx_buf = t->tx_buf;
  1014. void *rx_buf = t->rx_buf;
  1015. unsigned len = t->len;
  1016. mcspi = spi_master_get_devdata(master);
  1017. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  1018. if ((len && !(rx_buf || tx_buf))) {
  1019. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  1020. t->speed_hz,
  1021. len,
  1022. tx_buf ? "tx" : "",
  1023. rx_buf ? "rx" : "",
  1024. t->bits_per_word);
  1025. return -EINVAL;
  1026. }
  1027. if (len < DMA_MIN_BYTES)
  1028. goto skip_dma_map;
  1029. if (mcspi_dma->dma_tx && tx_buf != NULL) {
  1030. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  1031. len, DMA_TO_DEVICE);
  1032. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  1033. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1034. 'T', len);
  1035. return -EINVAL;
  1036. }
  1037. }
  1038. if (mcspi_dma->dma_rx && rx_buf != NULL) {
  1039. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  1040. DMA_FROM_DEVICE);
  1041. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  1042. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1043. 'R', len);
  1044. if (tx_buf != NULL)
  1045. dma_unmap_single(mcspi->dev, t->tx_dma,
  1046. len, DMA_TO_DEVICE);
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. skip_dma_map:
  1051. return omap2_mcspi_work_one(mcspi, spi, t);
  1052. }
  1053. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  1054. {
  1055. struct spi_master *master = mcspi->master;
  1056. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1057. int ret = 0;
  1058. ret = pm_runtime_get_sync(mcspi->dev);
  1059. if (ret < 0)
  1060. return ret;
  1061. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1062. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1063. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1064. omap2_mcspi_set_master_mode(master);
  1065. pm_runtime_mark_last_busy(mcspi->dev);
  1066. pm_runtime_put_autosuspend(mcspi->dev);
  1067. return 0;
  1068. }
  1069. static int omap_mcspi_runtime_resume(struct device *dev)
  1070. {
  1071. struct omap2_mcspi *mcspi;
  1072. struct spi_master *master;
  1073. master = dev_get_drvdata(dev);
  1074. mcspi = spi_master_get_devdata(master);
  1075. omap2_mcspi_restore_ctx(mcspi);
  1076. return 0;
  1077. }
  1078. static struct omap2_mcspi_platform_config omap2_pdata = {
  1079. .regs_offset = 0,
  1080. };
  1081. static struct omap2_mcspi_platform_config omap4_pdata = {
  1082. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1083. };
  1084. static const struct of_device_id omap_mcspi_of_match[] = {
  1085. {
  1086. .compatible = "ti,omap2-mcspi",
  1087. .data = &omap2_pdata,
  1088. },
  1089. {
  1090. .compatible = "ti,omap4-mcspi",
  1091. .data = &omap4_pdata,
  1092. },
  1093. { },
  1094. };
  1095. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1096. static int omap2_mcspi_probe(struct platform_device *pdev)
  1097. {
  1098. struct spi_master *master;
  1099. const struct omap2_mcspi_platform_config *pdata;
  1100. struct omap2_mcspi *mcspi;
  1101. struct resource *r;
  1102. int status = 0, i;
  1103. u32 regs_offset = 0;
  1104. static int bus_num = 1;
  1105. struct device_node *node = pdev->dev.of_node;
  1106. const struct of_device_id *match;
  1107. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  1108. if (master == NULL) {
  1109. dev_dbg(&pdev->dev, "master allocation failed\n");
  1110. return -ENOMEM;
  1111. }
  1112. /* the spi->mode bits understood by this driver: */
  1113. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1114. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1115. master->setup = omap2_mcspi_setup;
  1116. master->auto_runtime_pm = true;
  1117. master->transfer_one = omap2_mcspi_transfer_one;
  1118. master->cleanup = omap2_mcspi_cleanup;
  1119. master->dev.of_node = node;
  1120. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1121. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1122. platform_set_drvdata(pdev, master);
  1123. mcspi = spi_master_get_devdata(master);
  1124. mcspi->master = master;
  1125. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1126. if (match) {
  1127. u32 num_cs = 1; /* default number of chipselect */
  1128. pdata = match->data;
  1129. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1130. master->num_chipselect = num_cs;
  1131. master->bus_num = bus_num++;
  1132. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1133. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1134. } else {
  1135. pdata = dev_get_platdata(&pdev->dev);
  1136. master->num_chipselect = pdata->num_cs;
  1137. if (pdev->id != -1)
  1138. master->bus_num = pdev->id;
  1139. mcspi->pin_dir = pdata->pin_dir;
  1140. }
  1141. regs_offset = pdata->regs_offset;
  1142. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1143. if (r == NULL) {
  1144. status = -ENODEV;
  1145. goto free_master;
  1146. }
  1147. r->start += regs_offset;
  1148. r->end += regs_offset;
  1149. mcspi->phys = r->start;
  1150. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1151. if (IS_ERR(mcspi->base)) {
  1152. status = PTR_ERR(mcspi->base);
  1153. goto free_master;
  1154. }
  1155. mcspi->dev = &pdev->dev;
  1156. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1157. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1158. sizeof(struct omap2_mcspi_dma),
  1159. GFP_KERNEL);
  1160. if (mcspi->dma_channels == NULL) {
  1161. status = -ENOMEM;
  1162. goto free_master;
  1163. }
  1164. for (i = 0; i < master->num_chipselect; i++) {
  1165. char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
  1166. char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
  1167. struct resource *dma_res;
  1168. sprintf(dma_rx_ch_name, "rx%d", i);
  1169. if (!pdev->dev.of_node) {
  1170. dma_res =
  1171. platform_get_resource_byname(pdev,
  1172. IORESOURCE_DMA,
  1173. dma_rx_ch_name);
  1174. if (!dma_res) {
  1175. dev_dbg(&pdev->dev,
  1176. "cannot get DMA RX channel\n");
  1177. status = -ENODEV;
  1178. break;
  1179. }
  1180. mcspi->dma_channels[i].dma_rx_sync_dev =
  1181. dma_res->start;
  1182. }
  1183. sprintf(dma_tx_ch_name, "tx%d", i);
  1184. if (!pdev->dev.of_node) {
  1185. dma_res =
  1186. platform_get_resource_byname(pdev,
  1187. IORESOURCE_DMA,
  1188. dma_tx_ch_name);
  1189. if (!dma_res) {
  1190. dev_dbg(&pdev->dev,
  1191. "cannot get DMA TX channel\n");
  1192. status = -ENODEV;
  1193. break;
  1194. }
  1195. mcspi->dma_channels[i].dma_tx_sync_dev =
  1196. dma_res->start;
  1197. }
  1198. }
  1199. if (status < 0)
  1200. goto free_master;
  1201. pm_runtime_use_autosuspend(&pdev->dev);
  1202. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1203. pm_runtime_enable(&pdev->dev);
  1204. status = omap2_mcspi_master_setup(mcspi);
  1205. if (status < 0)
  1206. goto disable_pm;
  1207. status = devm_spi_register_master(&pdev->dev, master);
  1208. if (status < 0)
  1209. goto disable_pm;
  1210. return status;
  1211. disable_pm:
  1212. pm_runtime_disable(&pdev->dev);
  1213. free_master:
  1214. spi_master_put(master);
  1215. return status;
  1216. }
  1217. static int omap2_mcspi_remove(struct platform_device *pdev)
  1218. {
  1219. struct spi_master *master = platform_get_drvdata(pdev);
  1220. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1221. pm_runtime_put_sync(mcspi->dev);
  1222. pm_runtime_disable(&pdev->dev);
  1223. return 0;
  1224. }
  1225. /* work with hotplug and coldplug */
  1226. MODULE_ALIAS("platform:omap2_mcspi");
  1227. #ifdef CONFIG_SUSPEND
  1228. /*
  1229. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1230. * unactive state when driver was suspend, then force it to unactive state at
  1231. * wake up.
  1232. */
  1233. static int omap2_mcspi_resume(struct device *dev)
  1234. {
  1235. struct spi_master *master = dev_get_drvdata(dev);
  1236. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1237. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1238. struct omap2_mcspi_cs *cs;
  1239. pm_runtime_get_sync(mcspi->dev);
  1240. list_for_each_entry(cs, &ctx->cs, node) {
  1241. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1242. /*
  1243. * We need to toggle CS state for OMAP take this
  1244. * change in account.
  1245. */
  1246. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1247. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1248. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1249. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1250. }
  1251. }
  1252. pm_runtime_mark_last_busy(mcspi->dev);
  1253. pm_runtime_put_autosuspend(mcspi->dev);
  1254. return 0;
  1255. }
  1256. #else
  1257. #define omap2_mcspi_resume NULL
  1258. #endif
  1259. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1260. .resume = omap2_mcspi_resume,
  1261. .runtime_resume = omap_mcspi_runtime_resume,
  1262. };
  1263. static struct platform_driver omap2_mcspi_driver = {
  1264. .driver = {
  1265. .name = "omap2_mcspi",
  1266. .pm = &omap2_mcspi_pm_ops,
  1267. .of_match_table = omap_mcspi_of_match,
  1268. },
  1269. .probe = omap2_mcspi_probe,
  1270. .remove = omap2_mcspi_remove,
  1271. };
  1272. module_platform_driver(omap2_mcspi_driver);
  1273. MODULE_LICENSE("GPL");