spi-tegra114.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * SPI driver for NVIDIA's Tegra114 SPI Controller.
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/clk/tegra.h>
  20. #include <linux/completion.h>
  21. #include <linux/delay.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/err.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/kernel.h>
  30. #include <linux/kthread.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/of.h>
  35. #include <linux/of_device.h>
  36. #include <linux/spi/spi.h>
  37. #define SPI_COMMAND1 0x000
  38. #define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  39. #define SPI_PACKED (1 << 5)
  40. #define SPI_TX_EN (1 << 11)
  41. #define SPI_RX_EN (1 << 12)
  42. #define SPI_BOTH_EN_BYTE (1 << 13)
  43. #define SPI_BOTH_EN_BIT (1 << 14)
  44. #define SPI_LSBYTE_FE (1 << 15)
  45. #define SPI_LSBIT_FE (1 << 16)
  46. #define SPI_BIDIROE (1 << 17)
  47. #define SPI_IDLE_SDA_DRIVE_LOW (0 << 18)
  48. #define SPI_IDLE_SDA_DRIVE_HIGH (1 << 18)
  49. #define SPI_IDLE_SDA_PULL_LOW (2 << 18)
  50. #define SPI_IDLE_SDA_PULL_HIGH (3 << 18)
  51. #define SPI_IDLE_SDA_MASK (3 << 18)
  52. #define SPI_CS_SS_VAL (1 << 20)
  53. #define SPI_CS_SW_HW (1 << 21)
  54. /* SPI_CS_POL_INACTIVE bits are default high */
  55. #define SPI_CS_POL_INACTIVE 22
  56. #define SPI_CS_POL_INACTIVE_0 (1 << 22)
  57. #define SPI_CS_POL_INACTIVE_1 (1 << 23)
  58. #define SPI_CS_POL_INACTIVE_2 (1 << 24)
  59. #define SPI_CS_POL_INACTIVE_3 (1 << 25)
  60. #define SPI_CS_POL_INACTIVE_MASK (0xF << 22)
  61. #define SPI_CS_SEL_0 (0 << 26)
  62. #define SPI_CS_SEL_1 (1 << 26)
  63. #define SPI_CS_SEL_2 (2 << 26)
  64. #define SPI_CS_SEL_3 (3 << 26)
  65. #define SPI_CS_SEL_MASK (3 << 26)
  66. #define SPI_CS_SEL(x) (((x) & 0x3) << 26)
  67. #define SPI_CONTROL_MODE_0 (0 << 28)
  68. #define SPI_CONTROL_MODE_1 (1 << 28)
  69. #define SPI_CONTROL_MODE_2 (2 << 28)
  70. #define SPI_CONTROL_MODE_3 (3 << 28)
  71. #define SPI_CONTROL_MODE_MASK (3 << 28)
  72. #define SPI_MODE_SEL(x) (((x) & 0x3) << 28)
  73. #define SPI_M_S (1 << 30)
  74. #define SPI_PIO (1 << 31)
  75. #define SPI_COMMAND2 0x004
  76. #define SPI_TX_TAP_DELAY(x) (((x) & 0x3F) << 6)
  77. #define SPI_RX_TAP_DELAY(x) (((x) & 0x3F) << 0)
  78. #define SPI_CS_TIMING1 0x008
  79. #define SPI_SETUP_HOLD(setup, hold) (((setup) << 4) | (hold))
  80. #define SPI_CS_SETUP_HOLD(reg, cs, val) \
  81. ((((val) & 0xFFu) << ((cs) * 8)) | \
  82. ((reg) & ~(0xFFu << ((cs) * 8))))
  83. #define SPI_CS_TIMING2 0x00C
  84. #define CYCLES_BETWEEN_PACKETS_0(x) (((x) & 0x1F) << 0)
  85. #define CS_ACTIVE_BETWEEN_PACKETS_0 (1 << 5)
  86. #define CYCLES_BETWEEN_PACKETS_1(x) (((x) & 0x1F) << 8)
  87. #define CS_ACTIVE_BETWEEN_PACKETS_1 (1 << 13)
  88. #define CYCLES_BETWEEN_PACKETS_2(x) (((x) & 0x1F) << 16)
  89. #define CS_ACTIVE_BETWEEN_PACKETS_2 (1 << 21)
  90. #define CYCLES_BETWEEN_PACKETS_3(x) (((x) & 0x1F) << 24)
  91. #define CS_ACTIVE_BETWEEN_PACKETS_3 (1 << 29)
  92. #define SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(reg, cs, val) \
  93. (reg = (((val) & 0x1) << ((cs) * 8 + 5)) | \
  94. ((reg) & ~(1 << ((cs) * 8 + 5))))
  95. #define SPI_SET_CYCLES_BETWEEN_PACKETS(reg, cs, val) \
  96. (reg = (((val) & 0xF) << ((cs) * 8)) | \
  97. ((reg) & ~(0xF << ((cs) * 8))))
  98. #define SPI_TRANS_STATUS 0x010
  99. #define SPI_BLK_CNT(val) (((val) >> 0) & 0xFFFF)
  100. #define SPI_SLV_IDLE_COUNT(val) (((val) >> 16) & 0xFF)
  101. #define SPI_RDY (1 << 30)
  102. #define SPI_FIFO_STATUS 0x014
  103. #define SPI_RX_FIFO_EMPTY (1 << 0)
  104. #define SPI_RX_FIFO_FULL (1 << 1)
  105. #define SPI_TX_FIFO_EMPTY (1 << 2)
  106. #define SPI_TX_FIFO_FULL (1 << 3)
  107. #define SPI_RX_FIFO_UNF (1 << 4)
  108. #define SPI_RX_FIFO_OVF (1 << 5)
  109. #define SPI_TX_FIFO_UNF (1 << 6)
  110. #define SPI_TX_FIFO_OVF (1 << 7)
  111. #define SPI_ERR (1 << 8)
  112. #define SPI_TX_FIFO_FLUSH (1 << 14)
  113. #define SPI_RX_FIFO_FLUSH (1 << 15)
  114. #define SPI_TX_FIFO_EMPTY_COUNT(val) (((val) >> 16) & 0x7F)
  115. #define SPI_RX_FIFO_FULL_COUNT(val) (((val) >> 23) & 0x7F)
  116. #define SPI_FRAME_END (1 << 30)
  117. #define SPI_CS_INACTIVE (1 << 31)
  118. #define SPI_FIFO_ERROR (SPI_RX_FIFO_UNF | \
  119. SPI_RX_FIFO_OVF | SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF)
  120. #define SPI_FIFO_EMPTY (SPI_RX_FIFO_EMPTY | SPI_TX_FIFO_EMPTY)
  121. #define SPI_TX_DATA 0x018
  122. #define SPI_RX_DATA 0x01C
  123. #define SPI_DMA_CTL 0x020
  124. #define SPI_TX_TRIG_1 (0 << 15)
  125. #define SPI_TX_TRIG_4 (1 << 15)
  126. #define SPI_TX_TRIG_8 (2 << 15)
  127. #define SPI_TX_TRIG_16 (3 << 15)
  128. #define SPI_TX_TRIG_MASK (3 << 15)
  129. #define SPI_RX_TRIG_1 (0 << 19)
  130. #define SPI_RX_TRIG_4 (1 << 19)
  131. #define SPI_RX_TRIG_8 (2 << 19)
  132. #define SPI_RX_TRIG_16 (3 << 19)
  133. #define SPI_RX_TRIG_MASK (3 << 19)
  134. #define SPI_IE_TX (1 << 28)
  135. #define SPI_IE_RX (1 << 29)
  136. #define SPI_CONT (1 << 30)
  137. #define SPI_DMA (1 << 31)
  138. #define SPI_DMA_EN SPI_DMA
  139. #define SPI_DMA_BLK 0x024
  140. #define SPI_DMA_BLK_SET(x) (((x) & 0xFFFF) << 0)
  141. #define SPI_TX_FIFO 0x108
  142. #define SPI_RX_FIFO 0x188
  143. #define MAX_CHIP_SELECT 4
  144. #define SPI_FIFO_DEPTH 64
  145. #define DATA_DIR_TX (1 << 0)
  146. #define DATA_DIR_RX (1 << 1)
  147. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  148. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  149. #define TX_FIFO_EMPTY_COUNT_MAX SPI_TX_FIFO_EMPTY_COUNT(0x40)
  150. #define RX_FIFO_FULL_COUNT_ZERO SPI_RX_FIFO_FULL_COUNT(0)
  151. #define MAX_HOLD_CYCLES 16
  152. #define SPI_DEFAULT_SPEED 25000000
  153. #define MAX_CHIP_SELECT 4
  154. #define SPI_FIFO_DEPTH 64
  155. struct tegra_spi_data {
  156. struct device *dev;
  157. struct spi_master *master;
  158. spinlock_t lock;
  159. struct clk *clk;
  160. void __iomem *base;
  161. phys_addr_t phys;
  162. unsigned irq;
  163. int dma_req_sel;
  164. u32 spi_max_frequency;
  165. u32 cur_speed;
  166. struct spi_device *cur_spi;
  167. struct spi_device *cs_control;
  168. unsigned cur_pos;
  169. unsigned cur_len;
  170. unsigned words_per_32bit;
  171. unsigned bytes_per_word;
  172. unsigned curr_dma_words;
  173. unsigned cur_direction;
  174. unsigned cur_rx_pos;
  175. unsigned cur_tx_pos;
  176. unsigned dma_buf_size;
  177. unsigned max_buf_size;
  178. bool is_curr_dma_xfer;
  179. struct completion rx_dma_complete;
  180. struct completion tx_dma_complete;
  181. u32 tx_status;
  182. u32 rx_status;
  183. u32 status_reg;
  184. bool is_packed;
  185. unsigned long packed_size;
  186. u32 command1_reg;
  187. u32 dma_control_reg;
  188. u32 def_command1_reg;
  189. u32 spi_cs_timing;
  190. struct completion xfer_completion;
  191. struct spi_transfer *curr_xfer;
  192. struct dma_chan *rx_dma_chan;
  193. u32 *rx_dma_buf;
  194. dma_addr_t rx_dma_phys;
  195. struct dma_async_tx_descriptor *rx_dma_desc;
  196. struct dma_chan *tx_dma_chan;
  197. u32 *tx_dma_buf;
  198. dma_addr_t tx_dma_phys;
  199. struct dma_async_tx_descriptor *tx_dma_desc;
  200. };
  201. static int tegra_spi_runtime_suspend(struct device *dev);
  202. static int tegra_spi_runtime_resume(struct device *dev);
  203. static inline unsigned long tegra_spi_readl(struct tegra_spi_data *tspi,
  204. unsigned long reg)
  205. {
  206. return readl(tspi->base + reg);
  207. }
  208. static inline void tegra_spi_writel(struct tegra_spi_data *tspi,
  209. unsigned long val, unsigned long reg)
  210. {
  211. writel(val, tspi->base + reg);
  212. /* Read back register to make sure that register writes completed */
  213. if (reg != SPI_TX_FIFO)
  214. readl(tspi->base + SPI_COMMAND1);
  215. }
  216. static void tegra_spi_clear_status(struct tegra_spi_data *tspi)
  217. {
  218. unsigned long val;
  219. /* Write 1 to clear status register */
  220. val = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
  221. tegra_spi_writel(tspi, val, SPI_TRANS_STATUS);
  222. /* Clear fifo status error if any */
  223. val = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  224. if (val & SPI_ERR)
  225. tegra_spi_writel(tspi, SPI_ERR | SPI_FIFO_ERROR,
  226. SPI_FIFO_STATUS);
  227. }
  228. static unsigned tegra_spi_calculate_curr_xfer_param(
  229. struct spi_device *spi, struct tegra_spi_data *tspi,
  230. struct spi_transfer *t)
  231. {
  232. unsigned remain_len = t->len - tspi->cur_pos;
  233. unsigned max_word;
  234. unsigned bits_per_word = t->bits_per_word;
  235. unsigned max_len;
  236. unsigned total_fifo_words;
  237. tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;
  238. if (bits_per_word == 8 || bits_per_word == 16) {
  239. tspi->is_packed = 1;
  240. tspi->words_per_32bit = 32/bits_per_word;
  241. } else {
  242. tspi->is_packed = 0;
  243. tspi->words_per_32bit = 1;
  244. }
  245. if (tspi->is_packed) {
  246. max_len = min(remain_len, tspi->max_buf_size);
  247. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  248. total_fifo_words = (max_len + 3) / 4;
  249. } else {
  250. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  251. max_word = min(max_word, tspi->max_buf_size/4);
  252. tspi->curr_dma_words = max_word;
  253. total_fifo_words = max_word;
  254. }
  255. return total_fifo_words;
  256. }
  257. static unsigned tegra_spi_fill_tx_fifo_from_client_txbuf(
  258. struct tegra_spi_data *tspi, struct spi_transfer *t)
  259. {
  260. unsigned nbytes;
  261. unsigned tx_empty_count;
  262. unsigned long fifo_status;
  263. unsigned max_n_32bit;
  264. unsigned i, count;
  265. unsigned long x;
  266. unsigned int written_words;
  267. unsigned fifo_words_left;
  268. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  269. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  270. tx_empty_count = SPI_TX_FIFO_EMPTY_COUNT(fifo_status);
  271. if (tspi->is_packed) {
  272. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  273. written_words = min(fifo_words_left, tspi->curr_dma_words);
  274. nbytes = written_words * tspi->bytes_per_word;
  275. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  276. for (count = 0; count < max_n_32bit; count++) {
  277. x = 0;
  278. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  279. x |= (*tx_buf++) << (i*8);
  280. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  281. }
  282. } else {
  283. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  284. written_words = max_n_32bit;
  285. nbytes = written_words * tspi->bytes_per_word;
  286. for (count = 0; count < max_n_32bit; count++) {
  287. x = 0;
  288. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  289. i++, nbytes--)
  290. x |= ((*tx_buf++) << i*8);
  291. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  292. }
  293. }
  294. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  295. return written_words;
  296. }
  297. static unsigned int tegra_spi_read_rx_fifo_to_client_rxbuf(
  298. struct tegra_spi_data *tspi, struct spi_transfer *t)
  299. {
  300. unsigned rx_full_count;
  301. unsigned long fifo_status;
  302. unsigned i, count;
  303. unsigned long x;
  304. unsigned int read_words = 0;
  305. unsigned len;
  306. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  307. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  308. rx_full_count = SPI_RX_FIFO_FULL_COUNT(fifo_status);
  309. if (tspi->is_packed) {
  310. len = tspi->curr_dma_words * tspi->bytes_per_word;
  311. for (count = 0; count < rx_full_count; count++) {
  312. x = tegra_spi_readl(tspi, SPI_RX_FIFO);
  313. for (i = 0; len && (i < 4); i++, len--)
  314. *rx_buf++ = (x >> i*8) & 0xFF;
  315. }
  316. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  317. read_words += tspi->curr_dma_words;
  318. } else {
  319. unsigned int rx_mask;
  320. unsigned int bits_per_word = t->bits_per_word;
  321. rx_mask = (1 << bits_per_word) - 1;
  322. for (count = 0; count < rx_full_count; count++) {
  323. x = tegra_spi_readl(tspi, SPI_RX_FIFO);
  324. x &= rx_mask;
  325. for (i = 0; (i < tspi->bytes_per_word); i++)
  326. *rx_buf++ = (x >> (i*8)) & 0xFF;
  327. }
  328. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  329. read_words += rx_full_count;
  330. }
  331. return read_words;
  332. }
  333. static void tegra_spi_copy_client_txbuf_to_spi_txbuf(
  334. struct tegra_spi_data *tspi, struct spi_transfer *t)
  335. {
  336. unsigned len;
  337. /* Make the dma buffer to read by cpu */
  338. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  339. tspi->dma_buf_size, DMA_TO_DEVICE);
  340. if (tspi->is_packed) {
  341. len = tspi->curr_dma_words * tspi->bytes_per_word;
  342. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  343. } else {
  344. unsigned int i;
  345. unsigned int count;
  346. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  347. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  348. unsigned int x;
  349. for (count = 0; count < tspi->curr_dma_words; count++) {
  350. x = 0;
  351. for (i = 0; consume && (i < tspi->bytes_per_word);
  352. i++, consume--)
  353. x |= ((*tx_buf++) << i * 8);
  354. tspi->tx_dma_buf[count] = x;
  355. }
  356. }
  357. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  358. /* Make the dma buffer to read by dma */
  359. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  360. tspi->dma_buf_size, DMA_TO_DEVICE);
  361. }
  362. static void tegra_spi_copy_spi_rxbuf_to_client_rxbuf(
  363. struct tegra_spi_data *tspi, struct spi_transfer *t)
  364. {
  365. unsigned len;
  366. /* Make the dma buffer to read by cpu */
  367. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  368. tspi->dma_buf_size, DMA_FROM_DEVICE);
  369. if (tspi->is_packed) {
  370. len = tspi->curr_dma_words * tspi->bytes_per_word;
  371. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  372. } else {
  373. unsigned int i;
  374. unsigned int count;
  375. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  376. unsigned int x;
  377. unsigned int rx_mask;
  378. unsigned int bits_per_word = t->bits_per_word;
  379. rx_mask = (1 << bits_per_word) - 1;
  380. for (count = 0; count < tspi->curr_dma_words; count++) {
  381. x = tspi->rx_dma_buf[count];
  382. x &= rx_mask;
  383. for (i = 0; (i < tspi->bytes_per_word); i++)
  384. *rx_buf++ = (x >> (i*8)) & 0xFF;
  385. }
  386. }
  387. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  388. /* Make the dma buffer to read by dma */
  389. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  390. tspi->dma_buf_size, DMA_FROM_DEVICE);
  391. }
  392. static void tegra_spi_dma_complete(void *args)
  393. {
  394. struct completion *dma_complete = args;
  395. complete(dma_complete);
  396. }
  397. static int tegra_spi_start_tx_dma(struct tegra_spi_data *tspi, int len)
  398. {
  399. INIT_COMPLETION(tspi->tx_dma_complete);
  400. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  401. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  402. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  403. if (!tspi->tx_dma_desc) {
  404. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  405. return -EIO;
  406. }
  407. tspi->tx_dma_desc->callback = tegra_spi_dma_complete;
  408. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  409. dmaengine_submit(tspi->tx_dma_desc);
  410. dma_async_issue_pending(tspi->tx_dma_chan);
  411. return 0;
  412. }
  413. static int tegra_spi_start_rx_dma(struct tegra_spi_data *tspi, int len)
  414. {
  415. INIT_COMPLETION(tspi->rx_dma_complete);
  416. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  417. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  418. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  419. if (!tspi->rx_dma_desc) {
  420. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  421. return -EIO;
  422. }
  423. tspi->rx_dma_desc->callback = tegra_spi_dma_complete;
  424. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  425. dmaengine_submit(tspi->rx_dma_desc);
  426. dma_async_issue_pending(tspi->rx_dma_chan);
  427. return 0;
  428. }
  429. static int tegra_spi_start_dma_based_transfer(
  430. struct tegra_spi_data *tspi, struct spi_transfer *t)
  431. {
  432. unsigned long val;
  433. unsigned int len;
  434. int ret = 0;
  435. unsigned long status;
  436. /* Make sure that Rx and Tx fifo are empty */
  437. status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  438. if ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
  439. dev_err(tspi->dev,
  440. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  441. return -EIO;
  442. }
  443. val = SPI_DMA_BLK_SET(tspi->curr_dma_words - 1);
  444. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  445. if (tspi->is_packed)
  446. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  447. 4) * 4;
  448. else
  449. len = tspi->curr_dma_words * 4;
  450. /* Set attention level based on length of transfer */
  451. if (len & 0xF)
  452. val |= SPI_TX_TRIG_1 | SPI_RX_TRIG_1;
  453. else if (((len) >> 4) & 0x1)
  454. val |= SPI_TX_TRIG_4 | SPI_RX_TRIG_4;
  455. else
  456. val |= SPI_TX_TRIG_8 | SPI_RX_TRIG_8;
  457. if (tspi->cur_direction & DATA_DIR_TX)
  458. val |= SPI_IE_TX;
  459. if (tspi->cur_direction & DATA_DIR_RX)
  460. val |= SPI_IE_RX;
  461. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  462. tspi->dma_control_reg = val;
  463. if (tspi->cur_direction & DATA_DIR_TX) {
  464. tegra_spi_copy_client_txbuf_to_spi_txbuf(tspi, t);
  465. ret = tegra_spi_start_tx_dma(tspi, len);
  466. if (ret < 0) {
  467. dev_err(tspi->dev,
  468. "Starting tx dma failed, err %d\n", ret);
  469. return ret;
  470. }
  471. }
  472. if (tspi->cur_direction & DATA_DIR_RX) {
  473. /* Make the dma buffer to read by dma */
  474. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  475. tspi->dma_buf_size, DMA_FROM_DEVICE);
  476. ret = tegra_spi_start_rx_dma(tspi, len);
  477. if (ret < 0) {
  478. dev_err(tspi->dev,
  479. "Starting rx dma failed, err %d\n", ret);
  480. if (tspi->cur_direction & DATA_DIR_TX)
  481. dmaengine_terminate_all(tspi->tx_dma_chan);
  482. return ret;
  483. }
  484. }
  485. tspi->is_curr_dma_xfer = true;
  486. tspi->dma_control_reg = val;
  487. val |= SPI_DMA_EN;
  488. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  489. return ret;
  490. }
  491. static int tegra_spi_start_cpu_based_transfer(
  492. struct tegra_spi_data *tspi, struct spi_transfer *t)
  493. {
  494. unsigned long val;
  495. unsigned cur_words;
  496. if (tspi->cur_direction & DATA_DIR_TX)
  497. cur_words = tegra_spi_fill_tx_fifo_from_client_txbuf(tspi, t);
  498. else
  499. cur_words = tspi->curr_dma_words;
  500. val = SPI_DMA_BLK_SET(cur_words - 1);
  501. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  502. val = 0;
  503. if (tspi->cur_direction & DATA_DIR_TX)
  504. val |= SPI_IE_TX;
  505. if (tspi->cur_direction & DATA_DIR_RX)
  506. val |= SPI_IE_RX;
  507. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  508. tspi->dma_control_reg = val;
  509. tspi->is_curr_dma_xfer = false;
  510. val |= SPI_DMA_EN;
  511. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  512. return 0;
  513. }
  514. static int tegra_spi_init_dma_param(struct tegra_spi_data *tspi,
  515. bool dma_to_memory)
  516. {
  517. struct dma_chan *dma_chan;
  518. u32 *dma_buf;
  519. dma_addr_t dma_phys;
  520. int ret;
  521. struct dma_slave_config dma_sconfig;
  522. dma_cap_mask_t mask;
  523. dma_cap_zero(mask);
  524. dma_cap_set(DMA_SLAVE, mask);
  525. dma_chan = dma_request_channel(mask, NULL, NULL);
  526. if (!dma_chan) {
  527. dev_err(tspi->dev,
  528. "Dma channel is not available, will try later\n");
  529. return -EPROBE_DEFER;
  530. }
  531. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  532. &dma_phys, GFP_KERNEL);
  533. if (!dma_buf) {
  534. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  535. dma_release_channel(dma_chan);
  536. return -ENOMEM;
  537. }
  538. dma_sconfig.slave_id = tspi->dma_req_sel;
  539. if (dma_to_memory) {
  540. dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
  541. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  542. dma_sconfig.src_maxburst = 0;
  543. } else {
  544. dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
  545. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  546. dma_sconfig.dst_maxburst = 0;
  547. }
  548. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  549. if (ret)
  550. goto scrub;
  551. if (dma_to_memory) {
  552. tspi->rx_dma_chan = dma_chan;
  553. tspi->rx_dma_buf = dma_buf;
  554. tspi->rx_dma_phys = dma_phys;
  555. } else {
  556. tspi->tx_dma_chan = dma_chan;
  557. tspi->tx_dma_buf = dma_buf;
  558. tspi->tx_dma_phys = dma_phys;
  559. }
  560. return 0;
  561. scrub:
  562. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  563. dma_release_channel(dma_chan);
  564. return ret;
  565. }
  566. static void tegra_spi_deinit_dma_param(struct tegra_spi_data *tspi,
  567. bool dma_to_memory)
  568. {
  569. u32 *dma_buf;
  570. dma_addr_t dma_phys;
  571. struct dma_chan *dma_chan;
  572. if (dma_to_memory) {
  573. dma_buf = tspi->rx_dma_buf;
  574. dma_chan = tspi->rx_dma_chan;
  575. dma_phys = tspi->rx_dma_phys;
  576. tspi->rx_dma_chan = NULL;
  577. tspi->rx_dma_buf = NULL;
  578. } else {
  579. dma_buf = tspi->tx_dma_buf;
  580. dma_chan = tspi->tx_dma_chan;
  581. dma_phys = tspi->tx_dma_phys;
  582. tspi->tx_dma_buf = NULL;
  583. tspi->tx_dma_chan = NULL;
  584. }
  585. if (!dma_chan)
  586. return;
  587. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  588. dma_release_channel(dma_chan);
  589. }
  590. static unsigned long tegra_spi_setup_transfer_one(struct spi_device *spi,
  591. struct spi_transfer *t, bool is_first_of_msg)
  592. {
  593. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  594. u32 speed = t->speed_hz;
  595. u8 bits_per_word = t->bits_per_word;
  596. unsigned long command1;
  597. int req_mode;
  598. if (speed != tspi->cur_speed) {
  599. clk_set_rate(tspi->clk, speed);
  600. tspi->cur_speed = speed;
  601. }
  602. tspi->cur_spi = spi;
  603. tspi->cur_pos = 0;
  604. tspi->cur_rx_pos = 0;
  605. tspi->cur_tx_pos = 0;
  606. tspi->curr_xfer = t;
  607. if (is_first_of_msg) {
  608. tegra_spi_clear_status(tspi);
  609. command1 = tspi->def_command1_reg;
  610. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  611. command1 &= ~SPI_CONTROL_MODE_MASK;
  612. req_mode = spi->mode & 0x3;
  613. if (req_mode == SPI_MODE_0)
  614. command1 |= SPI_CONTROL_MODE_0;
  615. else if (req_mode == SPI_MODE_1)
  616. command1 |= SPI_CONTROL_MODE_1;
  617. else if (req_mode == SPI_MODE_2)
  618. command1 |= SPI_CONTROL_MODE_2;
  619. else if (req_mode == SPI_MODE_3)
  620. command1 |= SPI_CONTROL_MODE_3;
  621. if (tspi->cs_control) {
  622. if (tspi->cs_control != spi)
  623. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  624. tspi->cs_control = NULL;
  625. } else
  626. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  627. command1 |= SPI_CS_SW_HW;
  628. if (spi->mode & SPI_CS_HIGH)
  629. command1 |= SPI_CS_SS_VAL;
  630. else
  631. command1 &= ~SPI_CS_SS_VAL;
  632. tegra_spi_writel(tspi, 0, SPI_COMMAND2);
  633. } else {
  634. command1 = tspi->command1_reg;
  635. command1 &= ~SPI_BIT_LENGTH(~0);
  636. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  637. }
  638. return command1;
  639. }
  640. static int tegra_spi_start_transfer_one(struct spi_device *spi,
  641. struct spi_transfer *t, unsigned long command1)
  642. {
  643. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  644. unsigned total_fifo_words;
  645. int ret;
  646. total_fifo_words = tegra_spi_calculate_curr_xfer_param(spi, tspi, t);
  647. if (tspi->is_packed)
  648. command1 |= SPI_PACKED;
  649. command1 &= ~(SPI_CS_SEL_MASK | SPI_TX_EN | SPI_RX_EN);
  650. tspi->cur_direction = 0;
  651. if (t->rx_buf) {
  652. command1 |= SPI_RX_EN;
  653. tspi->cur_direction |= DATA_DIR_RX;
  654. }
  655. if (t->tx_buf) {
  656. command1 |= SPI_TX_EN;
  657. tspi->cur_direction |= DATA_DIR_TX;
  658. }
  659. command1 |= SPI_CS_SEL(spi->chip_select);
  660. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  661. tspi->command1_reg = command1;
  662. dev_dbg(tspi->dev, "The def 0x%x and written 0x%lx\n",
  663. tspi->def_command1_reg, command1);
  664. if (total_fifo_words > SPI_FIFO_DEPTH)
  665. ret = tegra_spi_start_dma_based_transfer(tspi, t);
  666. else
  667. ret = tegra_spi_start_cpu_based_transfer(tspi, t);
  668. return ret;
  669. }
  670. static int tegra_spi_setup(struct spi_device *spi)
  671. {
  672. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  673. unsigned long val;
  674. unsigned long flags;
  675. int ret;
  676. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  677. SPI_CS_POL_INACTIVE_0,
  678. SPI_CS_POL_INACTIVE_1,
  679. SPI_CS_POL_INACTIVE_2,
  680. SPI_CS_POL_INACTIVE_3,
  681. };
  682. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  683. spi->bits_per_word,
  684. spi->mode & SPI_CPOL ? "" : "~",
  685. spi->mode & SPI_CPHA ? "" : "~",
  686. spi->max_speed_hz);
  687. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  688. /* Set speed to the spi max fequency if spi device has not set */
  689. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  690. ret = pm_runtime_get_sync(tspi->dev);
  691. if (ret < 0) {
  692. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  693. return ret;
  694. }
  695. spin_lock_irqsave(&tspi->lock, flags);
  696. val = tspi->def_command1_reg;
  697. if (spi->mode & SPI_CS_HIGH)
  698. val &= ~cs_pol_bit[spi->chip_select];
  699. else
  700. val |= cs_pol_bit[spi->chip_select];
  701. tspi->def_command1_reg = val;
  702. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  703. spin_unlock_irqrestore(&tspi->lock, flags);
  704. pm_runtime_put(tspi->dev);
  705. return 0;
  706. }
  707. static void tegra_spi_transfer_delay(int delay)
  708. {
  709. if (!delay)
  710. return;
  711. if (delay >= 1000)
  712. mdelay(delay / 1000);
  713. udelay(delay % 1000);
  714. }
  715. static int tegra_spi_transfer_one_message(struct spi_master *master,
  716. struct spi_message *msg)
  717. {
  718. bool is_first_msg = true;
  719. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  720. struct spi_transfer *xfer;
  721. struct spi_device *spi = msg->spi;
  722. int ret;
  723. bool skip = false;
  724. msg->status = 0;
  725. msg->actual_length = 0;
  726. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  727. unsigned long cmd1;
  728. INIT_COMPLETION(tspi->xfer_completion);
  729. cmd1 = tegra_spi_setup_transfer_one(spi, xfer, is_first_msg);
  730. if (!xfer->len) {
  731. ret = 0;
  732. skip = true;
  733. goto complete_xfer;
  734. }
  735. ret = tegra_spi_start_transfer_one(spi, xfer, cmd1);
  736. if (ret < 0) {
  737. dev_err(tspi->dev,
  738. "spi can not start transfer, err %d\n", ret);
  739. goto complete_xfer;
  740. }
  741. is_first_msg = false;
  742. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  743. SPI_DMA_TIMEOUT);
  744. if (WARN_ON(ret == 0)) {
  745. dev_err(tspi->dev,
  746. "spi trasfer timeout, err %d\n", ret);
  747. ret = -EIO;
  748. goto complete_xfer;
  749. }
  750. if (tspi->tx_status || tspi->rx_status) {
  751. dev_err(tspi->dev, "Error in Transfer\n");
  752. ret = -EIO;
  753. goto complete_xfer;
  754. }
  755. msg->actual_length += xfer->len;
  756. complete_xfer:
  757. if (ret < 0 || skip) {
  758. tegra_spi_writel(tspi, tspi->def_command1_reg,
  759. SPI_COMMAND1);
  760. tegra_spi_transfer_delay(xfer->delay_usecs);
  761. goto exit;
  762. } else if (msg->transfers.prev == &xfer->transfer_list) {
  763. /* This is the last transfer in message */
  764. if (xfer->cs_change)
  765. tspi->cs_control = spi;
  766. else {
  767. tegra_spi_writel(tspi, tspi->def_command1_reg,
  768. SPI_COMMAND1);
  769. tegra_spi_transfer_delay(xfer->delay_usecs);
  770. }
  771. } else if (xfer->cs_change) {
  772. tegra_spi_writel(tspi, tspi->def_command1_reg,
  773. SPI_COMMAND1);
  774. tegra_spi_transfer_delay(xfer->delay_usecs);
  775. }
  776. }
  777. ret = 0;
  778. exit:
  779. msg->status = ret;
  780. spi_finalize_current_message(master);
  781. return ret;
  782. }
  783. static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
  784. {
  785. struct spi_transfer *t = tspi->curr_xfer;
  786. unsigned long flags;
  787. spin_lock_irqsave(&tspi->lock, flags);
  788. if (tspi->tx_status || tspi->rx_status) {
  789. dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
  790. tspi->status_reg);
  791. dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
  792. tspi->command1_reg, tspi->dma_control_reg);
  793. tegra_periph_reset_assert(tspi->clk);
  794. udelay(2);
  795. tegra_periph_reset_deassert(tspi->clk);
  796. complete(&tspi->xfer_completion);
  797. goto exit;
  798. }
  799. if (tspi->cur_direction & DATA_DIR_RX)
  800. tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
  801. if (tspi->cur_direction & DATA_DIR_TX)
  802. tspi->cur_pos = tspi->cur_tx_pos;
  803. else
  804. tspi->cur_pos = tspi->cur_rx_pos;
  805. if (tspi->cur_pos == t->len) {
  806. complete(&tspi->xfer_completion);
  807. goto exit;
  808. }
  809. tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  810. tegra_spi_start_cpu_based_transfer(tspi, t);
  811. exit:
  812. spin_unlock_irqrestore(&tspi->lock, flags);
  813. return IRQ_HANDLED;
  814. }
  815. static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
  816. {
  817. struct spi_transfer *t = tspi->curr_xfer;
  818. long wait_status;
  819. int err = 0;
  820. unsigned total_fifo_words;
  821. unsigned long flags;
  822. /* Abort dmas if any error */
  823. if (tspi->cur_direction & DATA_DIR_TX) {
  824. if (tspi->tx_status) {
  825. dmaengine_terminate_all(tspi->tx_dma_chan);
  826. err += 1;
  827. } else {
  828. wait_status = wait_for_completion_interruptible_timeout(
  829. &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
  830. if (wait_status <= 0) {
  831. dmaengine_terminate_all(tspi->tx_dma_chan);
  832. dev_err(tspi->dev, "TxDma Xfer failed\n");
  833. err += 1;
  834. }
  835. }
  836. }
  837. if (tspi->cur_direction & DATA_DIR_RX) {
  838. if (tspi->rx_status) {
  839. dmaengine_terminate_all(tspi->rx_dma_chan);
  840. err += 2;
  841. } else {
  842. wait_status = wait_for_completion_interruptible_timeout(
  843. &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
  844. if (wait_status <= 0) {
  845. dmaengine_terminate_all(tspi->rx_dma_chan);
  846. dev_err(tspi->dev, "RxDma Xfer failed\n");
  847. err += 2;
  848. }
  849. }
  850. }
  851. spin_lock_irqsave(&tspi->lock, flags);
  852. if (err) {
  853. dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
  854. tspi->status_reg);
  855. dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
  856. tspi->command1_reg, tspi->dma_control_reg);
  857. tegra_periph_reset_assert(tspi->clk);
  858. udelay(2);
  859. tegra_periph_reset_deassert(tspi->clk);
  860. complete(&tspi->xfer_completion);
  861. spin_unlock_irqrestore(&tspi->lock, flags);
  862. return IRQ_HANDLED;
  863. }
  864. if (tspi->cur_direction & DATA_DIR_RX)
  865. tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  866. if (tspi->cur_direction & DATA_DIR_TX)
  867. tspi->cur_pos = tspi->cur_tx_pos;
  868. else
  869. tspi->cur_pos = tspi->cur_rx_pos;
  870. if (tspi->cur_pos == t->len) {
  871. complete(&tspi->xfer_completion);
  872. goto exit;
  873. }
  874. /* Continue transfer in current message */
  875. total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
  876. tspi, t);
  877. if (total_fifo_words > SPI_FIFO_DEPTH)
  878. err = tegra_spi_start_dma_based_transfer(tspi, t);
  879. else
  880. err = tegra_spi_start_cpu_based_transfer(tspi, t);
  881. exit:
  882. spin_unlock_irqrestore(&tspi->lock, flags);
  883. return IRQ_HANDLED;
  884. }
  885. static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
  886. {
  887. struct tegra_spi_data *tspi = context_data;
  888. if (!tspi->is_curr_dma_xfer)
  889. return handle_cpu_based_xfer(tspi);
  890. return handle_dma_based_xfer(tspi);
  891. }
  892. static irqreturn_t tegra_spi_isr(int irq, void *context_data)
  893. {
  894. struct tegra_spi_data *tspi = context_data;
  895. tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  896. if (tspi->cur_direction & DATA_DIR_TX)
  897. tspi->tx_status = tspi->status_reg &
  898. (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
  899. if (tspi->cur_direction & DATA_DIR_RX)
  900. tspi->rx_status = tspi->status_reg &
  901. (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
  902. tegra_spi_clear_status(tspi);
  903. return IRQ_WAKE_THREAD;
  904. }
  905. static void tegra_spi_parse_dt(struct platform_device *pdev,
  906. struct tegra_spi_data *tspi)
  907. {
  908. struct device_node *np = pdev->dev.of_node;
  909. u32 of_dma[2];
  910. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  911. of_dma, 2) >= 0)
  912. tspi->dma_req_sel = of_dma[1];
  913. if (of_property_read_u32(np, "spi-max-frequency",
  914. &tspi->spi_max_frequency))
  915. tspi->spi_max_frequency = 25000000; /* 25MHz */
  916. }
  917. static struct of_device_id tegra_spi_of_match[] = {
  918. { .compatible = "nvidia,tegra114-spi", },
  919. {}
  920. };
  921. MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
  922. static int tegra_spi_probe(struct platform_device *pdev)
  923. {
  924. struct spi_master *master;
  925. struct tegra_spi_data *tspi;
  926. struct resource *r;
  927. int ret, spi_irq;
  928. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  929. if (!master) {
  930. dev_err(&pdev->dev, "master allocation failed\n");
  931. return -ENOMEM;
  932. }
  933. platform_set_drvdata(pdev, master);
  934. tspi = spi_master_get_devdata(master);
  935. /* Parse DT */
  936. tegra_spi_parse_dt(pdev, tspi);
  937. /* the spi->mode bits understood by this driver: */
  938. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  939. master->setup = tegra_spi_setup;
  940. master->transfer_one_message = tegra_spi_transfer_one_message;
  941. master->num_chipselect = MAX_CHIP_SELECT;
  942. master->bus_num = -1;
  943. master->auto_runtime_pm = true;
  944. tspi->master = master;
  945. tspi->dev = &pdev->dev;
  946. spin_lock_init(&tspi->lock);
  947. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  948. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  949. if (IS_ERR(tspi->base)) {
  950. ret = PTR_ERR(tspi->base);
  951. goto exit_free_master;
  952. }
  953. tspi->phys = r->start;
  954. spi_irq = platform_get_irq(pdev, 0);
  955. tspi->irq = spi_irq;
  956. ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
  957. tegra_spi_isr_thread, IRQF_ONESHOT,
  958. dev_name(&pdev->dev), tspi);
  959. if (ret < 0) {
  960. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  961. tspi->irq);
  962. goto exit_free_master;
  963. }
  964. tspi->clk = devm_clk_get(&pdev->dev, "spi");
  965. if (IS_ERR(tspi->clk)) {
  966. dev_err(&pdev->dev, "can not get clock\n");
  967. ret = PTR_ERR(tspi->clk);
  968. goto exit_free_irq;
  969. }
  970. tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
  971. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  972. if (tspi->dma_req_sel) {
  973. ret = tegra_spi_init_dma_param(tspi, true);
  974. if (ret < 0) {
  975. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  976. goto exit_free_irq;
  977. }
  978. ret = tegra_spi_init_dma_param(tspi, false);
  979. if (ret < 0) {
  980. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  981. goto exit_rx_dma_free;
  982. }
  983. tspi->max_buf_size = tspi->dma_buf_size;
  984. init_completion(&tspi->tx_dma_complete);
  985. init_completion(&tspi->rx_dma_complete);
  986. }
  987. init_completion(&tspi->xfer_completion);
  988. pm_runtime_enable(&pdev->dev);
  989. if (!pm_runtime_enabled(&pdev->dev)) {
  990. ret = tegra_spi_runtime_resume(&pdev->dev);
  991. if (ret)
  992. goto exit_pm_disable;
  993. }
  994. ret = pm_runtime_get_sync(&pdev->dev);
  995. if (ret < 0) {
  996. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  997. goto exit_pm_disable;
  998. }
  999. tspi->def_command1_reg = SPI_M_S;
  1000. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  1001. pm_runtime_put(&pdev->dev);
  1002. master->dev.of_node = pdev->dev.of_node;
  1003. ret = spi_register_master(master);
  1004. if (ret < 0) {
  1005. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1006. goto exit_pm_disable;
  1007. }
  1008. return ret;
  1009. exit_pm_disable:
  1010. pm_runtime_disable(&pdev->dev);
  1011. if (!pm_runtime_status_suspended(&pdev->dev))
  1012. tegra_spi_runtime_suspend(&pdev->dev);
  1013. tegra_spi_deinit_dma_param(tspi, false);
  1014. exit_rx_dma_free:
  1015. tegra_spi_deinit_dma_param(tspi, true);
  1016. exit_free_irq:
  1017. free_irq(spi_irq, tspi);
  1018. exit_free_master:
  1019. spi_master_put(master);
  1020. return ret;
  1021. }
  1022. static int tegra_spi_remove(struct platform_device *pdev)
  1023. {
  1024. struct spi_master *master = platform_get_drvdata(pdev);
  1025. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1026. free_irq(tspi->irq, tspi);
  1027. spi_unregister_master(master);
  1028. if (tspi->tx_dma_chan)
  1029. tegra_spi_deinit_dma_param(tspi, false);
  1030. if (tspi->rx_dma_chan)
  1031. tegra_spi_deinit_dma_param(tspi, true);
  1032. pm_runtime_disable(&pdev->dev);
  1033. if (!pm_runtime_status_suspended(&pdev->dev))
  1034. tegra_spi_runtime_suspend(&pdev->dev);
  1035. return 0;
  1036. }
  1037. #ifdef CONFIG_PM_SLEEP
  1038. static int tegra_spi_suspend(struct device *dev)
  1039. {
  1040. struct spi_master *master = dev_get_drvdata(dev);
  1041. return spi_master_suspend(master);
  1042. }
  1043. static int tegra_spi_resume(struct device *dev)
  1044. {
  1045. struct spi_master *master = dev_get_drvdata(dev);
  1046. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1047. int ret;
  1048. ret = pm_runtime_get_sync(dev);
  1049. if (ret < 0) {
  1050. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1051. return ret;
  1052. }
  1053. tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
  1054. pm_runtime_put(dev);
  1055. return spi_master_resume(master);
  1056. }
  1057. #endif
  1058. static int tegra_spi_runtime_suspend(struct device *dev)
  1059. {
  1060. struct spi_master *master = dev_get_drvdata(dev);
  1061. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1062. /* Flush all write which are in PPSB queue by reading back */
  1063. tegra_spi_readl(tspi, SPI_COMMAND1);
  1064. clk_disable_unprepare(tspi->clk);
  1065. return 0;
  1066. }
  1067. static int tegra_spi_runtime_resume(struct device *dev)
  1068. {
  1069. struct spi_master *master = dev_get_drvdata(dev);
  1070. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1071. int ret;
  1072. ret = clk_prepare_enable(tspi->clk);
  1073. if (ret < 0) {
  1074. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1075. return ret;
  1076. }
  1077. return 0;
  1078. }
  1079. static const struct dev_pm_ops tegra_spi_pm_ops = {
  1080. SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
  1081. tegra_spi_runtime_resume, NULL)
  1082. SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
  1083. };
  1084. static struct platform_driver tegra_spi_driver = {
  1085. .driver = {
  1086. .name = "spi-tegra114",
  1087. .owner = THIS_MODULE,
  1088. .pm = &tegra_spi_pm_ops,
  1089. .of_match_table = tegra_spi_of_match,
  1090. },
  1091. .probe = tegra_spi_probe,
  1092. .remove = tegra_spi_remove,
  1093. };
  1094. module_platform_driver(tegra_spi_driver);
  1095. MODULE_ALIAS("platform:spi-tegra114");
  1096. MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
  1097. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1098. MODULE_LICENSE("GPL v2");