spi-omap2-mcspi.c 37 KB

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