spi-omap2-mcspi.c 34 KB

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