omap2_mcspi.c 32 KB

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