spi-pxa2xx.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  4. * Copyright (C) 2013, 2021 Intel Corporation
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/of.h>
  22. #include <linux/pci.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/property.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/pxa2xx_spi.h>
  28. #include <linux/spi/spi.h>
  29. #include "spi-pxa2xx.h"
  30. MODULE_AUTHOR("Stephen Street");
  31. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS("platform:pxa2xx-spi");
  34. #define TIMOUT_DFLT 1000
  35. /*
  36. * For testing SSCR1 changes that require SSP restart, basically
  37. * everything except the service and interrupt enables, the PXA270 developer
  38. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  39. * list, but the PXA255 developer manual says all bits without really meaning
  40. * the service and interrupt enables.
  41. */
  42. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  43. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  44. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  45. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  46. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  47. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  48. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  49. | QUARK_X1000_SSCR1_EFWR \
  50. | QUARK_X1000_SSCR1_RFT \
  51. | QUARK_X1000_SSCR1_TFT \
  52. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  53. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  54. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  55. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  56. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  57. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  60. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  61. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  62. #define LPSS_CAPS_CS_EN_SHIFT 9
  63. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  64. #define LPSS_PRIV_CLOCK_GATE 0x38
  65. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
  66. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
  67. struct lpss_config {
  68. /* LPSS offset from drv_data->ioaddr */
  69. unsigned offset;
  70. /* Register offsets from drv_data->lpss_base or -1 */
  71. int reg_general;
  72. int reg_ssp;
  73. int reg_cs_ctrl;
  74. int reg_capabilities;
  75. /* FIFO thresholds */
  76. u32 rx_threshold;
  77. u32 tx_threshold_lo;
  78. u32 tx_threshold_hi;
  79. /* Chip select control */
  80. unsigned cs_sel_shift;
  81. unsigned cs_sel_mask;
  82. unsigned cs_num;
  83. /* Quirks */
  84. unsigned cs_clk_stays_gated : 1;
  85. };
  86. /* Keep these sorted with enum pxa_ssp_type */
  87. static const struct lpss_config lpss_platforms[] = {
  88. { /* LPSS_LPT_SSP */
  89. .offset = 0x800,
  90. .reg_general = 0x08,
  91. .reg_ssp = 0x0c,
  92. .reg_cs_ctrl = 0x18,
  93. .reg_capabilities = -1,
  94. .rx_threshold = 64,
  95. .tx_threshold_lo = 160,
  96. .tx_threshold_hi = 224,
  97. },
  98. { /* LPSS_BYT_SSP */
  99. .offset = 0x400,
  100. .reg_general = 0x08,
  101. .reg_ssp = 0x0c,
  102. .reg_cs_ctrl = 0x18,
  103. .reg_capabilities = -1,
  104. .rx_threshold = 64,
  105. .tx_threshold_lo = 160,
  106. .tx_threshold_hi = 224,
  107. },
  108. { /* LPSS_BSW_SSP */
  109. .offset = 0x400,
  110. .reg_general = 0x08,
  111. .reg_ssp = 0x0c,
  112. .reg_cs_ctrl = 0x18,
  113. .reg_capabilities = -1,
  114. .rx_threshold = 64,
  115. .tx_threshold_lo = 160,
  116. .tx_threshold_hi = 224,
  117. .cs_sel_shift = 2,
  118. .cs_sel_mask = 1 << 2,
  119. .cs_num = 2,
  120. },
  121. { /* LPSS_SPT_SSP */
  122. .offset = 0x200,
  123. .reg_general = -1,
  124. .reg_ssp = 0x20,
  125. .reg_cs_ctrl = 0x24,
  126. .reg_capabilities = -1,
  127. .rx_threshold = 1,
  128. .tx_threshold_lo = 32,
  129. .tx_threshold_hi = 56,
  130. },
  131. { /* LPSS_BXT_SSP */
  132. .offset = 0x200,
  133. .reg_general = -1,
  134. .reg_ssp = 0x20,
  135. .reg_cs_ctrl = 0x24,
  136. .reg_capabilities = 0xfc,
  137. .rx_threshold = 1,
  138. .tx_threshold_lo = 16,
  139. .tx_threshold_hi = 48,
  140. .cs_sel_shift = 8,
  141. .cs_sel_mask = 3 << 8,
  142. .cs_clk_stays_gated = true,
  143. },
  144. { /* LPSS_CNL_SSP */
  145. .offset = 0x200,
  146. .reg_general = -1,
  147. .reg_ssp = 0x20,
  148. .reg_cs_ctrl = 0x24,
  149. .reg_capabilities = 0xfc,
  150. .rx_threshold = 1,
  151. .tx_threshold_lo = 32,
  152. .tx_threshold_hi = 56,
  153. .cs_sel_shift = 8,
  154. .cs_sel_mask = 3 << 8,
  155. .cs_clk_stays_gated = true,
  156. },
  157. };
  158. static inline const struct lpss_config
  159. *lpss_get_config(const struct driver_data *drv_data)
  160. {
  161. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  162. }
  163. static bool is_lpss_ssp(const struct driver_data *drv_data)
  164. {
  165. switch (drv_data->ssp_type) {
  166. case LPSS_LPT_SSP:
  167. case LPSS_BYT_SSP:
  168. case LPSS_BSW_SSP:
  169. case LPSS_SPT_SSP:
  170. case LPSS_BXT_SSP:
  171. case LPSS_CNL_SSP:
  172. return true;
  173. default:
  174. return false;
  175. }
  176. }
  177. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  178. {
  179. return drv_data->ssp_type == QUARK_X1000_SSP;
  180. }
  181. static bool is_mmp2_ssp(const struct driver_data *drv_data)
  182. {
  183. return drv_data->ssp_type == MMP2_SSP;
  184. }
  185. static bool is_mrfld_ssp(const struct driver_data *drv_data)
  186. {
  187. return drv_data->ssp_type == MRFLD_SSP;
  188. }
  189. static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
  190. {
  191. if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
  192. pxa2xx_spi_write(drv_data, reg, value & mask);
  193. }
  194. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  195. {
  196. switch (drv_data->ssp_type) {
  197. case QUARK_X1000_SSP:
  198. return QUARK_X1000_SSCR1_CHANGE_MASK;
  199. case CE4100_SSP:
  200. return CE4100_SSCR1_CHANGE_MASK;
  201. default:
  202. return SSCR1_CHANGE_MASK;
  203. }
  204. }
  205. static u32
  206. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  207. {
  208. switch (drv_data->ssp_type) {
  209. case QUARK_X1000_SSP:
  210. return RX_THRESH_QUARK_X1000_DFLT;
  211. case CE4100_SSP:
  212. return RX_THRESH_CE4100_DFLT;
  213. default:
  214. return RX_THRESH_DFLT;
  215. }
  216. }
  217. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  218. {
  219. u32 mask;
  220. switch (drv_data->ssp_type) {
  221. case QUARK_X1000_SSP:
  222. mask = QUARK_X1000_SSSR_TFL_MASK;
  223. break;
  224. case CE4100_SSP:
  225. mask = CE4100_SSSR_TFL_MASK;
  226. break;
  227. default:
  228. mask = SSSR_TFL_MASK;
  229. break;
  230. }
  231. return read_SSSR_bits(drv_data, mask) == mask;
  232. }
  233. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  234. u32 *sccr1_reg)
  235. {
  236. u32 mask;
  237. switch (drv_data->ssp_type) {
  238. case QUARK_X1000_SSP:
  239. mask = QUARK_X1000_SSCR1_RFT;
  240. break;
  241. case CE4100_SSP:
  242. mask = CE4100_SSCR1_RFT;
  243. break;
  244. default:
  245. mask = SSCR1_RFT;
  246. break;
  247. }
  248. *sccr1_reg &= ~mask;
  249. }
  250. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  251. u32 *sccr1_reg, u32 threshold)
  252. {
  253. switch (drv_data->ssp_type) {
  254. case QUARK_X1000_SSP:
  255. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  256. break;
  257. case CE4100_SSP:
  258. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  259. break;
  260. default:
  261. *sccr1_reg |= SSCR1_RxTresh(threshold);
  262. break;
  263. }
  264. }
  265. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  266. u32 clk_div, u8 bits)
  267. {
  268. switch (drv_data->ssp_type) {
  269. case QUARK_X1000_SSP:
  270. return clk_div
  271. | QUARK_X1000_SSCR0_Motorola
  272. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
  273. default:
  274. return clk_div
  275. | SSCR0_Motorola
  276. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  277. | (bits > 16 ? SSCR0_EDSS : 0);
  278. }
  279. }
  280. /*
  281. * Read and write LPSS SSP private registers. Caller must first check that
  282. * is_lpss_ssp() returns true before these can be called.
  283. */
  284. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  285. {
  286. WARN_ON(!drv_data->lpss_base);
  287. return readl(drv_data->lpss_base + offset);
  288. }
  289. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  290. unsigned offset, u32 value)
  291. {
  292. WARN_ON(!drv_data->lpss_base);
  293. writel(value, drv_data->lpss_base + offset);
  294. }
  295. /*
  296. * lpss_ssp_setup - perform LPSS SSP specific setup
  297. * @drv_data: pointer to the driver private data
  298. *
  299. * Perform LPSS SSP specific setup. This function must be called first if
  300. * one is going to use LPSS SSP private registers.
  301. */
  302. static void lpss_ssp_setup(struct driver_data *drv_data)
  303. {
  304. const struct lpss_config *config;
  305. u32 value;
  306. config = lpss_get_config(drv_data);
  307. drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
  308. /* Enable software chip select control */
  309. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  310. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  311. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  312. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  313. /* Enable multiblock DMA transfers */
  314. if (drv_data->controller_info->enable_dma) {
  315. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  316. if (config->reg_general >= 0) {
  317. value = __lpss_ssp_read_priv(drv_data,
  318. config->reg_general);
  319. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  320. __lpss_ssp_write_priv(drv_data,
  321. config->reg_general, value);
  322. }
  323. }
  324. }
  325. static void lpss_ssp_select_cs(struct spi_device *spi,
  326. const struct lpss_config *config)
  327. {
  328. struct driver_data *drv_data =
  329. spi_controller_get_devdata(spi->controller);
  330. u32 value, cs;
  331. if (!config->cs_sel_mask)
  332. return;
  333. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  334. cs = spi->chip_select;
  335. cs <<= config->cs_sel_shift;
  336. if (cs != (value & config->cs_sel_mask)) {
  337. /*
  338. * When switching another chip select output active the
  339. * output must be selected first and wait 2 ssp_clk cycles
  340. * before changing state to active. Otherwise a short
  341. * glitch will occur on the previous chip select since
  342. * output select is latched but state control is not.
  343. */
  344. value &= ~config->cs_sel_mask;
  345. value |= cs;
  346. __lpss_ssp_write_priv(drv_data,
  347. config->reg_cs_ctrl, value);
  348. ndelay(1000000000 /
  349. (drv_data->controller->max_speed_hz / 2));
  350. }
  351. }
  352. static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
  353. {
  354. struct driver_data *drv_data =
  355. spi_controller_get_devdata(spi->controller);
  356. const struct lpss_config *config;
  357. u32 value;
  358. config = lpss_get_config(drv_data);
  359. if (enable)
  360. lpss_ssp_select_cs(spi, config);
  361. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  362. if (enable)
  363. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  364. else
  365. value |= LPSS_CS_CONTROL_CS_HIGH;
  366. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  367. if (config->cs_clk_stays_gated) {
  368. u32 clkgate;
  369. /*
  370. * Changing CS alone when dynamic clock gating is on won't
  371. * actually flip CS at that time. This ruins SPI transfers
  372. * that specify delays, or have no data. Toggle the clock mode
  373. * to force on briefly to poke the CS pin to move.
  374. */
  375. clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
  376. value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
  377. LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
  378. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
  379. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
  380. }
  381. }
  382. static void cs_assert(struct spi_device *spi)
  383. {
  384. struct driver_data *drv_data =
  385. spi_controller_get_devdata(spi->controller);
  386. if (drv_data->ssp_type == CE4100_SSP) {
  387. pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
  388. return;
  389. }
  390. if (is_lpss_ssp(drv_data))
  391. lpss_ssp_cs_control(spi, true);
  392. }
  393. static void cs_deassert(struct spi_device *spi)
  394. {
  395. struct driver_data *drv_data =
  396. spi_controller_get_devdata(spi->controller);
  397. unsigned long timeout;
  398. if (drv_data->ssp_type == CE4100_SSP)
  399. return;
  400. /* Wait until SSP becomes idle before deasserting the CS */
  401. timeout = jiffies + msecs_to_jiffies(10);
  402. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  403. !time_after(jiffies, timeout))
  404. cpu_relax();
  405. if (is_lpss_ssp(drv_data))
  406. lpss_ssp_cs_control(spi, false);
  407. }
  408. static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
  409. {
  410. if (level)
  411. cs_deassert(spi);
  412. else
  413. cs_assert(spi);
  414. }
  415. int pxa2xx_spi_flush(struct driver_data *drv_data)
  416. {
  417. unsigned long limit = loops_per_jiffy << 1;
  418. do {
  419. while (read_SSSR_bits(drv_data, SSSR_RNE))
  420. pxa2xx_spi_read(drv_data, SSDR);
  421. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  422. write_SSSR_CS(drv_data, SSSR_ROR);
  423. return limit;
  424. }
  425. static void pxa2xx_spi_off(struct driver_data *drv_data)
  426. {
  427. /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
  428. if (is_mmp2_ssp(drv_data))
  429. return;
  430. pxa_ssp_disable(drv_data->ssp);
  431. }
  432. static int null_writer(struct driver_data *drv_data)
  433. {
  434. u8 n_bytes = drv_data->n_bytes;
  435. if (pxa2xx_spi_txfifo_full(drv_data)
  436. || (drv_data->tx == drv_data->tx_end))
  437. return 0;
  438. pxa2xx_spi_write(drv_data, SSDR, 0);
  439. drv_data->tx += n_bytes;
  440. return 1;
  441. }
  442. static int null_reader(struct driver_data *drv_data)
  443. {
  444. u8 n_bytes = drv_data->n_bytes;
  445. while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
  446. pxa2xx_spi_read(drv_data, SSDR);
  447. drv_data->rx += n_bytes;
  448. }
  449. return drv_data->rx == drv_data->rx_end;
  450. }
  451. static int u8_writer(struct driver_data *drv_data)
  452. {
  453. if (pxa2xx_spi_txfifo_full(drv_data)
  454. || (drv_data->tx == drv_data->tx_end))
  455. return 0;
  456. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  457. ++drv_data->tx;
  458. return 1;
  459. }
  460. static int u8_reader(struct driver_data *drv_data)
  461. {
  462. while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
  463. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  464. ++drv_data->rx;
  465. }
  466. return drv_data->rx == drv_data->rx_end;
  467. }
  468. static int u16_writer(struct driver_data *drv_data)
  469. {
  470. if (pxa2xx_spi_txfifo_full(drv_data)
  471. || (drv_data->tx == drv_data->tx_end))
  472. return 0;
  473. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  474. drv_data->tx += 2;
  475. return 1;
  476. }
  477. static int u16_reader(struct driver_data *drv_data)
  478. {
  479. while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
  480. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  481. drv_data->rx += 2;
  482. }
  483. return drv_data->rx == drv_data->rx_end;
  484. }
  485. static int u32_writer(struct driver_data *drv_data)
  486. {
  487. if (pxa2xx_spi_txfifo_full(drv_data)
  488. || (drv_data->tx == drv_data->tx_end))
  489. return 0;
  490. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  491. drv_data->tx += 4;
  492. return 1;
  493. }
  494. static int u32_reader(struct driver_data *drv_data)
  495. {
  496. while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
  497. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  498. drv_data->rx += 4;
  499. }
  500. return drv_data->rx == drv_data->rx_end;
  501. }
  502. static void reset_sccr1(struct driver_data *drv_data)
  503. {
  504. u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
  505. struct chip_data *chip;
  506. if (drv_data->controller->cur_msg) {
  507. chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
  508. threshold = chip->threshold;
  509. } else {
  510. threshold = 0;
  511. }
  512. switch (drv_data->ssp_type) {
  513. case QUARK_X1000_SSP:
  514. mask |= QUARK_X1000_SSCR1_RFT;
  515. break;
  516. case CE4100_SSP:
  517. mask |= CE4100_SSCR1_RFT;
  518. break;
  519. default:
  520. mask |= SSCR1_RFT;
  521. break;
  522. }
  523. pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
  524. }
  525. static void int_stop_and_reset(struct driver_data *drv_data)
  526. {
  527. /* Clear and disable interrupts */
  528. write_SSSR_CS(drv_data, drv_data->clear_sr);
  529. reset_sccr1(drv_data);
  530. if (pxa25x_ssp_comp(drv_data))
  531. return;
  532. pxa2xx_spi_write(drv_data, SSTO, 0);
  533. }
  534. static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
  535. {
  536. int_stop_and_reset(drv_data);
  537. pxa2xx_spi_flush(drv_data);
  538. pxa2xx_spi_off(drv_data);
  539. dev_err(drv_data->ssp->dev, "%s\n", msg);
  540. drv_data->controller->cur_msg->status = err;
  541. spi_finalize_current_transfer(drv_data->controller);
  542. }
  543. static void int_transfer_complete(struct driver_data *drv_data)
  544. {
  545. int_stop_and_reset(drv_data);
  546. spi_finalize_current_transfer(drv_data->controller);
  547. }
  548. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  549. {
  550. u32 irq_status;
  551. irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
  552. if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
  553. irq_status &= ~SSSR_TFS;
  554. if (irq_status & SSSR_ROR) {
  555. int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
  556. return IRQ_HANDLED;
  557. }
  558. if (irq_status & SSSR_TUR) {
  559. int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
  560. return IRQ_HANDLED;
  561. }
  562. if (irq_status & SSSR_TINT) {
  563. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  564. if (drv_data->read(drv_data)) {
  565. int_transfer_complete(drv_data);
  566. return IRQ_HANDLED;
  567. }
  568. }
  569. /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
  570. do {
  571. if (drv_data->read(drv_data)) {
  572. int_transfer_complete(drv_data);
  573. return IRQ_HANDLED;
  574. }
  575. } while (drv_data->write(drv_data));
  576. if (drv_data->read(drv_data)) {
  577. int_transfer_complete(drv_data);
  578. return IRQ_HANDLED;
  579. }
  580. if (drv_data->tx == drv_data->tx_end) {
  581. u32 bytes_left;
  582. u32 sccr1_reg;
  583. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  584. sccr1_reg &= ~SSCR1_TIE;
  585. /*
  586. * PXA25x_SSP has no timeout, set up Rx threshold for
  587. * the remaining Rx bytes.
  588. */
  589. if (pxa25x_ssp_comp(drv_data)) {
  590. u32 rx_thre;
  591. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  592. bytes_left = drv_data->rx_end - drv_data->rx;
  593. switch (drv_data->n_bytes) {
  594. case 4:
  595. bytes_left >>= 2;
  596. break;
  597. case 2:
  598. bytes_left >>= 1;
  599. break;
  600. }
  601. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  602. if (rx_thre > bytes_left)
  603. rx_thre = bytes_left;
  604. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  605. }
  606. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  607. }
  608. /* We did something */
  609. return IRQ_HANDLED;
  610. }
  611. static void handle_bad_msg(struct driver_data *drv_data)
  612. {
  613. int_stop_and_reset(drv_data);
  614. pxa2xx_spi_off(drv_data);
  615. dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
  616. }
  617. static irqreturn_t ssp_int(int irq, void *dev_id)
  618. {
  619. struct driver_data *drv_data = dev_id;
  620. u32 sccr1_reg;
  621. u32 mask = drv_data->mask_sr;
  622. u32 status;
  623. /*
  624. * The IRQ might be shared with other peripherals so we must first
  625. * check that are we RPM suspended or not. If we are we assume that
  626. * the IRQ was not for us (we shouldn't be RPM suspended when the
  627. * interrupt is enabled).
  628. */
  629. if (pm_runtime_suspended(drv_data->ssp->dev))
  630. return IRQ_NONE;
  631. /*
  632. * If the device is not yet in RPM suspended state and we get an
  633. * interrupt that is meant for another device, check if status bits
  634. * are all set to one. That means that the device is already
  635. * powered off.
  636. */
  637. status = pxa2xx_spi_read(drv_data, SSSR);
  638. if (status == ~0)
  639. return IRQ_NONE;
  640. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  641. /* Ignore possible writes if we don't need to write */
  642. if (!(sccr1_reg & SSCR1_TIE))
  643. mask &= ~SSSR_TFS;
  644. /* Ignore RX timeout interrupt if it is disabled */
  645. if (!(sccr1_reg & SSCR1_TINTE))
  646. mask &= ~SSSR_TINT;
  647. if (!(status & mask))
  648. return IRQ_NONE;
  649. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  650. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  651. if (!drv_data->controller->cur_msg) {
  652. handle_bad_msg(drv_data);
  653. /* Never fail */
  654. return IRQ_HANDLED;
  655. }
  656. return drv_data->transfer_handler(drv_data);
  657. }
  658. /*
  659. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  660. * input frequency by fractions of 2^24. It also has a divider by 5.
  661. *
  662. * There are formulas to get baud rate value for given input frequency and
  663. * divider parameters, such as DDS_CLK_RATE and SCR:
  664. *
  665. * Fsys = 200MHz
  666. *
  667. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  668. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  669. *
  670. * DDS_CLK_RATE either 2^n or 2^n / 5.
  671. * SCR is in range 0 .. 255
  672. *
  673. * Divisor = 5^i * 2^j * 2 * k
  674. * i = [0, 1] i = 1 iff j = 0 or j > 3
  675. * j = [0, 23] j = 0 iff i = 1
  676. * k = [1, 256]
  677. * Special case: j = 0, i = 1: Divisor = 2 / 5
  678. *
  679. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  680. * are:
  681. * Case 1: 2^n, n = [0, 23]
  682. * Case 2: 2^24 * 2 / 5 (0x666666)
  683. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  684. *
  685. * In all cases the lowest possible value is better.
  686. *
  687. * The function calculates parameters for all cases and chooses the one closest
  688. * to the asked baud rate.
  689. */
  690. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  691. {
  692. unsigned long xtal = 200000000;
  693. unsigned long fref = xtal / 2; /* mandatory division by 2,
  694. see (2) */
  695. /* case 3 */
  696. unsigned long fref1 = fref / 2; /* case 1 */
  697. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  698. unsigned long scale;
  699. unsigned long q, q1, q2;
  700. long r, r1, r2;
  701. u32 mul;
  702. /* Case 1 */
  703. /* Set initial value for DDS_CLK_RATE */
  704. mul = (1 << 24) >> 1;
  705. /* Calculate initial quot */
  706. q1 = DIV_ROUND_UP(fref1, rate);
  707. /* Scale q1 if it's too big */
  708. if (q1 > 256) {
  709. /* Scale q1 to range [1, 512] */
  710. scale = fls_long(q1 - 1);
  711. if (scale > 9) {
  712. q1 >>= scale - 9;
  713. mul >>= scale - 9;
  714. }
  715. /* Round the result if we have a remainder */
  716. q1 += q1 & 1;
  717. }
  718. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  719. scale = __ffs(q1);
  720. q1 >>= scale;
  721. mul >>= scale;
  722. /* Get the remainder */
  723. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  724. /* Case 2 */
  725. q2 = DIV_ROUND_UP(fref2, rate);
  726. r2 = abs(fref2 / q2 - rate);
  727. /*
  728. * Choose the best between two: less remainder we have the better. We
  729. * can't go case 2 if q2 is greater than 256 since SCR register can
  730. * hold only values 0 .. 255.
  731. */
  732. if (r2 >= r1 || q2 > 256) {
  733. /* case 1 is better */
  734. r = r1;
  735. q = q1;
  736. } else {
  737. /* case 2 is better */
  738. r = r2;
  739. q = q2;
  740. mul = (1 << 24) * 2 / 5;
  741. }
  742. /* Check case 3 only if the divisor is big enough */
  743. if (fref / rate >= 80) {
  744. u64 fssp;
  745. u32 m;
  746. /* Calculate initial quot */
  747. q1 = DIV_ROUND_UP(fref, rate);
  748. m = (1 << 24) / q1;
  749. /* Get the remainder */
  750. fssp = (u64)fref * m;
  751. do_div(fssp, 1 << 24);
  752. r1 = abs(fssp - rate);
  753. /* Choose this one if it suits better */
  754. if (r1 < r) {
  755. /* case 3 is better */
  756. q = 1;
  757. mul = m;
  758. }
  759. }
  760. *dds = mul;
  761. return q - 1;
  762. }
  763. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  764. {
  765. unsigned long ssp_clk = drv_data->controller->max_speed_hz;
  766. const struct ssp_device *ssp = drv_data->ssp;
  767. rate = min_t(int, ssp_clk, rate);
  768. /*
  769. * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
  770. * that the SSP transmission rate can be greater than the device rate.
  771. */
  772. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  773. return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
  774. else
  775. return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
  776. }
  777. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  778. int rate)
  779. {
  780. struct chip_data *chip =
  781. spi_get_ctldata(drv_data->controller->cur_msg->spi);
  782. unsigned int clk_div;
  783. switch (drv_data->ssp_type) {
  784. case QUARK_X1000_SSP:
  785. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  786. break;
  787. default:
  788. clk_div = ssp_get_clk_div(drv_data, rate);
  789. break;
  790. }
  791. return clk_div << 8;
  792. }
  793. static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
  794. struct spi_device *spi,
  795. struct spi_transfer *xfer)
  796. {
  797. struct chip_data *chip = spi_get_ctldata(spi);
  798. return chip->enable_dma &&
  799. xfer->len <= MAX_DMA_LEN &&
  800. xfer->len >= chip->dma_burst_size;
  801. }
  802. static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
  803. struct spi_device *spi,
  804. struct spi_transfer *transfer)
  805. {
  806. struct driver_data *drv_data = spi_controller_get_devdata(controller);
  807. struct spi_message *message = controller->cur_msg;
  808. struct chip_data *chip = spi_get_ctldata(spi);
  809. u32 dma_thresh = chip->dma_threshold;
  810. u32 dma_burst = chip->dma_burst_size;
  811. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  812. u32 clk_div;
  813. u8 bits;
  814. u32 speed;
  815. u32 cr0;
  816. u32 cr1;
  817. int err;
  818. int dma_mapped;
  819. /* Check if we can DMA this transfer */
  820. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  821. /* Reject already-mapped transfers; PIO won't always work */
  822. if (message->is_dma_mapped
  823. || transfer->rx_dma || transfer->tx_dma) {
  824. dev_err(&spi->dev,
  825. "Mapped transfer length of %u is greater than %d\n",
  826. transfer->len, MAX_DMA_LEN);
  827. return -EINVAL;
  828. }
  829. /* Warn ... we force this to PIO mode */
  830. dev_warn_ratelimited(&spi->dev,
  831. "DMA disabled for transfer length %u greater than %d\n",
  832. transfer->len, MAX_DMA_LEN);
  833. }
  834. /* Setup the transfer state based on the type of transfer */
  835. if (pxa2xx_spi_flush(drv_data) == 0) {
  836. dev_err(&spi->dev, "Flush failed\n");
  837. return -EIO;
  838. }
  839. drv_data->tx = (void *)transfer->tx_buf;
  840. drv_data->tx_end = drv_data->tx + transfer->len;
  841. drv_data->rx = transfer->rx_buf;
  842. drv_data->rx_end = drv_data->rx + transfer->len;
  843. /* Change speed and bit per word on a per transfer */
  844. bits = transfer->bits_per_word;
  845. speed = transfer->speed_hz;
  846. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  847. if (bits <= 8) {
  848. drv_data->n_bytes = 1;
  849. drv_data->read = drv_data->rx ? u8_reader : null_reader;
  850. drv_data->write = drv_data->tx ? u8_writer : null_writer;
  851. } else if (bits <= 16) {
  852. drv_data->n_bytes = 2;
  853. drv_data->read = drv_data->rx ? u16_reader : null_reader;
  854. drv_data->write = drv_data->tx ? u16_writer : null_writer;
  855. } else if (bits <= 32) {
  856. drv_data->n_bytes = 4;
  857. drv_data->read = drv_data->rx ? u32_reader : null_reader;
  858. drv_data->write = drv_data->tx ? u32_writer : null_writer;
  859. }
  860. /*
  861. * If bits per word is changed in DMA mode, then must check
  862. * the thresholds and burst also.
  863. */
  864. if (chip->enable_dma) {
  865. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  866. spi,
  867. bits, &dma_burst,
  868. &dma_thresh))
  869. dev_warn_ratelimited(&spi->dev,
  870. "DMA burst size reduced to match bits_per_word\n");
  871. }
  872. dma_mapped = controller->can_dma &&
  873. controller->can_dma(controller, spi, transfer) &&
  874. controller->cur_msg_mapped;
  875. if (dma_mapped) {
  876. /* Ensure we have the correct interrupt handler */
  877. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  878. err = pxa2xx_spi_dma_prepare(drv_data, transfer);
  879. if (err)
  880. return err;
  881. /* Clear status and start DMA engine */
  882. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  883. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  884. pxa2xx_spi_dma_start(drv_data);
  885. } else {
  886. /* Ensure we have the correct interrupt handler */
  887. drv_data->transfer_handler = interrupt_transfer;
  888. /* Clear status */
  889. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  890. write_SSSR_CS(drv_data, drv_data->clear_sr);
  891. }
  892. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  893. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  894. if (!pxa25x_ssp_comp(drv_data))
  895. dev_dbg(&spi->dev, "%u Hz actual, %s\n",
  896. controller->max_speed_hz
  897. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  898. dma_mapped ? "DMA" : "PIO");
  899. else
  900. dev_dbg(&spi->dev, "%u Hz actual, %s\n",
  901. controller->max_speed_hz / 2
  902. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  903. dma_mapped ? "DMA" : "PIO");
  904. if (is_lpss_ssp(drv_data)) {
  905. pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
  906. pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
  907. }
  908. if (is_mrfld_ssp(drv_data)) {
  909. u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
  910. u32 thresh = 0;
  911. thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
  912. thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
  913. pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
  914. }
  915. if (is_quark_x1000_ssp(drv_data))
  916. pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
  917. /* Stop the SSP */
  918. if (!is_mmp2_ssp(drv_data))
  919. pxa_ssp_disable(drv_data->ssp);
  920. if (!pxa25x_ssp_comp(drv_data))
  921. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  922. /* First set CR1 without interrupt and service enables */
  923. pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
  924. /* See if we need to reload the configuration registers */
  925. pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
  926. /* Restart the SSP */
  927. pxa_ssp_enable(drv_data->ssp);
  928. if (is_mmp2_ssp(drv_data)) {
  929. u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
  930. if (tx_level) {
  931. /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
  932. dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
  933. if (tx_level > transfer->len)
  934. tx_level = transfer->len;
  935. drv_data->tx += tx_level;
  936. }
  937. }
  938. if (spi_controller_is_slave(controller)) {
  939. while (drv_data->write(drv_data))
  940. ;
  941. if (drv_data->gpiod_ready) {
  942. gpiod_set_value(drv_data->gpiod_ready, 1);
  943. udelay(1);
  944. gpiod_set_value(drv_data->gpiod_ready, 0);
  945. }
  946. }
  947. /*
  948. * Release the data by enabling service requests and interrupts,
  949. * without changing any mode bits.
  950. */
  951. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  952. return 1;
  953. }
  954. static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
  955. {
  956. struct driver_data *drv_data = spi_controller_get_devdata(controller);
  957. int_error_stop(drv_data, "transfer aborted", -EINTR);
  958. return 0;
  959. }
  960. static void pxa2xx_spi_handle_err(struct spi_controller *controller,
  961. struct spi_message *msg)
  962. {
  963. struct driver_data *drv_data = spi_controller_get_devdata(controller);
  964. int_stop_and_reset(drv_data);
  965. /* Disable the SSP */
  966. pxa2xx_spi_off(drv_data);
  967. /*
  968. * Stop the DMA if running. Note DMA callback handler may have unset
  969. * the dma_running already, which is fine as stopping is not needed
  970. * then but we shouldn't rely this flag for anything else than
  971. * stopping. For instance to differentiate between PIO and DMA
  972. * transfers.
  973. */
  974. if (atomic_read(&drv_data->dma_running))
  975. pxa2xx_spi_dma_stop(drv_data);
  976. }
  977. static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
  978. {
  979. struct driver_data *drv_data = spi_controller_get_devdata(controller);
  980. /* Disable the SSP now */
  981. pxa2xx_spi_off(drv_data);
  982. return 0;
  983. }
  984. static int setup(struct spi_device *spi)
  985. {
  986. struct pxa2xx_spi_chip *chip_info;
  987. struct chip_data *chip;
  988. const struct lpss_config *config;
  989. struct driver_data *drv_data =
  990. spi_controller_get_devdata(spi->controller);
  991. uint tx_thres, tx_hi_thres, rx_thres;
  992. switch (drv_data->ssp_type) {
  993. case QUARK_X1000_SSP:
  994. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  995. tx_hi_thres = 0;
  996. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  997. break;
  998. case MRFLD_SSP:
  999. tx_thres = TX_THRESH_MRFLD_DFLT;
  1000. tx_hi_thres = 0;
  1001. rx_thres = RX_THRESH_MRFLD_DFLT;
  1002. break;
  1003. case CE4100_SSP:
  1004. tx_thres = TX_THRESH_CE4100_DFLT;
  1005. tx_hi_thres = 0;
  1006. rx_thres = RX_THRESH_CE4100_DFLT;
  1007. break;
  1008. case LPSS_LPT_SSP:
  1009. case LPSS_BYT_SSP:
  1010. case LPSS_BSW_SSP:
  1011. case LPSS_SPT_SSP:
  1012. case LPSS_BXT_SSP:
  1013. case LPSS_CNL_SSP:
  1014. config = lpss_get_config(drv_data);
  1015. tx_thres = config->tx_threshold_lo;
  1016. tx_hi_thres = config->tx_threshold_hi;
  1017. rx_thres = config->rx_threshold;
  1018. break;
  1019. default:
  1020. tx_hi_thres = 0;
  1021. if (spi_controller_is_slave(drv_data->controller)) {
  1022. tx_thres = 1;
  1023. rx_thres = 2;
  1024. } else {
  1025. tx_thres = TX_THRESH_DFLT;
  1026. rx_thres = RX_THRESH_DFLT;
  1027. }
  1028. break;
  1029. }
  1030. /* Only allocate on the first setup */
  1031. chip = spi_get_ctldata(spi);
  1032. if (!chip) {
  1033. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1034. if (!chip)
  1035. return -ENOMEM;
  1036. if (drv_data->ssp_type == CE4100_SSP) {
  1037. if (spi->chip_select > 4) {
  1038. dev_err(&spi->dev,
  1039. "failed setup: cs number must not be > 4.\n");
  1040. kfree(chip);
  1041. return -EINVAL;
  1042. }
  1043. }
  1044. chip->enable_dma = drv_data->controller_info->enable_dma;
  1045. chip->timeout = TIMOUT_DFLT;
  1046. }
  1047. /*
  1048. * Protocol drivers may change the chip settings, so...
  1049. * if chip_info exists, use it.
  1050. */
  1051. chip_info = spi->controller_data;
  1052. /* chip_info isn't always needed */
  1053. if (chip_info) {
  1054. if (chip_info->timeout)
  1055. chip->timeout = chip_info->timeout;
  1056. if (chip_info->tx_threshold)
  1057. tx_thres = chip_info->tx_threshold;
  1058. if (chip_info->tx_hi_threshold)
  1059. tx_hi_thres = chip_info->tx_hi_threshold;
  1060. if (chip_info->rx_threshold)
  1061. rx_thres = chip_info->rx_threshold;
  1062. chip->dma_threshold = 0;
  1063. }
  1064. chip->cr1 = 0;
  1065. if (spi_controller_is_slave(drv_data->controller)) {
  1066. chip->cr1 |= SSCR1_SCFR;
  1067. chip->cr1 |= SSCR1_SCLKDIR;
  1068. chip->cr1 |= SSCR1_SFRMDIR;
  1069. chip->cr1 |= SSCR1_SPH;
  1070. }
  1071. if (is_lpss_ssp(drv_data)) {
  1072. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1073. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
  1074. SSITF_TxHiThresh(tx_hi_thres);
  1075. }
  1076. if (is_mrfld_ssp(drv_data)) {
  1077. chip->lpss_rx_threshold = rx_thres;
  1078. chip->lpss_tx_threshold = tx_thres;
  1079. }
  1080. /*
  1081. * Set DMA burst and threshold outside of chip_info path so that if
  1082. * chip_info goes away after setting chip->enable_dma, the burst and
  1083. * threshold can still respond to changes in bits_per_word.
  1084. */
  1085. if (chip->enable_dma) {
  1086. /* Set up legal burst and threshold for DMA */
  1087. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1088. spi->bits_per_word,
  1089. &chip->dma_burst_size,
  1090. &chip->dma_threshold)) {
  1091. dev_warn(&spi->dev,
  1092. "in setup: DMA burst size reduced to match bits_per_word\n");
  1093. }
  1094. dev_dbg(&spi->dev,
  1095. "in setup: DMA burst size set to %u\n",
  1096. chip->dma_burst_size);
  1097. }
  1098. switch (drv_data->ssp_type) {
  1099. case QUARK_X1000_SSP:
  1100. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1101. & QUARK_X1000_SSCR1_RFT)
  1102. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1103. & QUARK_X1000_SSCR1_TFT);
  1104. break;
  1105. case CE4100_SSP:
  1106. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1107. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1108. break;
  1109. default:
  1110. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1111. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1112. break;
  1113. }
  1114. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1115. chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
  1116. ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
  1117. if (spi->mode & SPI_LOOP)
  1118. chip->cr1 |= SSCR1_LBM;
  1119. spi_set_ctldata(spi, chip);
  1120. return 0;
  1121. }
  1122. static void cleanup(struct spi_device *spi)
  1123. {
  1124. struct chip_data *chip = spi_get_ctldata(spi);
  1125. kfree(chip);
  1126. }
  1127. #ifdef CONFIG_ACPI
  1128. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1129. { "INT33C0", LPSS_LPT_SSP },
  1130. { "INT33C1", LPSS_LPT_SSP },
  1131. { "INT3430", LPSS_LPT_SSP },
  1132. { "INT3431", LPSS_LPT_SSP },
  1133. { "80860F0E", LPSS_BYT_SSP },
  1134. { "8086228E", LPSS_BSW_SSP },
  1135. { },
  1136. };
  1137. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1138. #endif
  1139. /*
  1140. * PCI IDs of compound devices that integrate both host controller and private
  1141. * integrated DMA engine. Please note these are not used in module
  1142. * autoloading and probing in this module but matching the LPSS SSP type.
  1143. */
  1144. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1145. /* SPT-LP */
  1146. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1147. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1148. /* SPT-H */
  1149. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1150. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1151. /* KBL-H */
  1152. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1153. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1154. /* CML-V */
  1155. { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
  1156. { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
  1157. /* BXT A-Step */
  1158. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1159. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1160. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1161. /* BXT B-Step */
  1162. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1163. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1164. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1165. /* GLK */
  1166. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1167. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1168. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1169. /* ICL-LP */
  1170. { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
  1171. { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
  1172. { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
  1173. /* EHL */
  1174. { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
  1175. { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
  1176. { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
  1177. /* JSL */
  1178. { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
  1179. { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
  1180. { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
  1181. /* TGL-H */
  1182. { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
  1183. { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
  1184. { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
  1185. { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
  1186. /* ADL-P */
  1187. { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
  1188. { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
  1189. { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
  1190. /* ADL-M */
  1191. { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
  1192. { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
  1193. { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
  1194. /* APL */
  1195. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1196. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1197. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1198. /* RPL-S */
  1199. { PCI_VDEVICE(INTEL, 0x7a2a), LPSS_CNL_SSP },
  1200. { PCI_VDEVICE(INTEL, 0x7a2b), LPSS_CNL_SSP },
  1201. { PCI_VDEVICE(INTEL, 0x7a79), LPSS_CNL_SSP },
  1202. { PCI_VDEVICE(INTEL, 0x7a7b), LPSS_CNL_SSP },
  1203. /* ADL-S */
  1204. { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
  1205. { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
  1206. { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
  1207. { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
  1208. /* MTL-P */
  1209. { PCI_VDEVICE(INTEL, 0x7e27), LPSS_CNL_SSP },
  1210. { PCI_VDEVICE(INTEL, 0x7e30), LPSS_CNL_SSP },
  1211. { PCI_VDEVICE(INTEL, 0x7e46), LPSS_CNL_SSP },
  1212. /* CNL-LP */
  1213. { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
  1214. { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
  1215. { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
  1216. /* CNL-H */
  1217. { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
  1218. { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
  1219. { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
  1220. /* CML-LP */
  1221. { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
  1222. { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
  1223. { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
  1224. /* CML-H */
  1225. { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
  1226. { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
  1227. { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
  1228. /* TGL-LP */
  1229. { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
  1230. { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
  1231. { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
  1232. { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
  1233. { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
  1234. { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
  1235. { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
  1236. { },
  1237. };
  1238. static const struct of_device_id pxa2xx_spi_of_match[] = {
  1239. { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
  1240. {},
  1241. };
  1242. MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
  1243. #ifdef CONFIG_PCI
  1244. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1245. {
  1246. return param == chan->device->dev;
  1247. }
  1248. #endif /* CONFIG_PCI */
  1249. static struct pxa2xx_spi_controller *
  1250. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1251. {
  1252. struct pxa2xx_spi_controller *pdata;
  1253. struct device *dev = &pdev->dev;
  1254. struct device *parent = dev->parent;
  1255. struct ssp_device *ssp;
  1256. struct resource *res;
  1257. struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
  1258. const struct pci_device_id *pcidev_id = NULL;
  1259. enum pxa_ssp_type type;
  1260. const void *match;
  1261. int status;
  1262. u64 uid;
  1263. if (pcidev)
  1264. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
  1265. match = device_get_match_data(&pdev->dev);
  1266. if (match)
  1267. type = (enum pxa_ssp_type)match;
  1268. else if (pcidev_id)
  1269. type = (enum pxa_ssp_type)pcidev_id->driver_data;
  1270. else
  1271. return ERR_PTR(-EINVAL);
  1272. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1273. if (!pdata)
  1274. return ERR_PTR(-ENOMEM);
  1275. ssp = &pdata->ssp;
  1276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1277. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1278. if (IS_ERR(ssp->mmio_base))
  1279. return ERR_CAST(ssp->mmio_base);
  1280. ssp->phys_base = res->start;
  1281. #ifdef CONFIG_PCI
  1282. if (pcidev_id) {
  1283. pdata->tx_param = parent;
  1284. pdata->rx_param = parent;
  1285. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1286. }
  1287. #endif
  1288. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1289. if (IS_ERR(ssp->clk))
  1290. return ERR_CAST(ssp->clk);
  1291. ssp->irq = platform_get_irq(pdev, 0);
  1292. if (ssp->irq < 0)
  1293. return ERR_PTR(ssp->irq);
  1294. ssp->type = type;
  1295. ssp->dev = &pdev->dev;
  1296. status = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
  1297. if (status)
  1298. ssp->port_id = -1;
  1299. else
  1300. ssp->port_id = uid;
  1301. pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
  1302. pdata->num_chipselect = 1;
  1303. pdata->enable_dma = true;
  1304. pdata->dma_burst_size = 1;
  1305. return pdata;
  1306. }
  1307. static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
  1308. unsigned int cs)
  1309. {
  1310. struct driver_data *drv_data = spi_controller_get_devdata(controller);
  1311. if (has_acpi_companion(drv_data->ssp->dev)) {
  1312. switch (drv_data->ssp_type) {
  1313. /*
  1314. * For Atoms the ACPI DeviceSelection used by the Windows
  1315. * driver starts from 1 instead of 0 so translate it here
  1316. * to match what Linux expects.
  1317. */
  1318. case LPSS_BYT_SSP:
  1319. case LPSS_BSW_SSP:
  1320. return cs - 1;
  1321. default:
  1322. break;
  1323. }
  1324. }
  1325. return cs;
  1326. }
  1327. static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
  1328. {
  1329. return MAX_DMA_LEN;
  1330. }
  1331. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1332. {
  1333. struct device *dev = &pdev->dev;
  1334. struct pxa2xx_spi_controller *platform_info;
  1335. struct spi_controller *controller;
  1336. struct driver_data *drv_data;
  1337. struct ssp_device *ssp;
  1338. const struct lpss_config *config;
  1339. int status;
  1340. u32 tmp;
  1341. platform_info = dev_get_platdata(dev);
  1342. if (!platform_info) {
  1343. platform_info = pxa2xx_spi_init_pdata(pdev);
  1344. if (IS_ERR(platform_info)) {
  1345. dev_err(&pdev->dev, "missing platform data\n");
  1346. return PTR_ERR(platform_info);
  1347. }
  1348. }
  1349. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1350. if (!ssp)
  1351. ssp = &platform_info->ssp;
  1352. if (!ssp->mmio_base) {
  1353. dev_err(&pdev->dev, "failed to get SSP\n");
  1354. return -ENODEV;
  1355. }
  1356. if (platform_info->is_slave)
  1357. controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
  1358. else
  1359. controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
  1360. if (!controller) {
  1361. dev_err(&pdev->dev, "cannot alloc spi_controller\n");
  1362. status = -ENOMEM;
  1363. goto out_error_controller_alloc;
  1364. }
  1365. drv_data = spi_controller_get_devdata(controller);
  1366. drv_data->controller = controller;
  1367. drv_data->controller_info = platform_info;
  1368. drv_data->ssp = ssp;
  1369. device_set_node(&controller->dev, dev_fwnode(dev));
  1370. /* The spi->mode bits understood by this driver: */
  1371. controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1372. controller->bus_num = ssp->port_id;
  1373. controller->dma_alignment = DMA_ALIGNMENT;
  1374. controller->cleanup = cleanup;
  1375. controller->setup = setup;
  1376. controller->set_cs = pxa2xx_spi_set_cs;
  1377. controller->transfer_one = pxa2xx_spi_transfer_one;
  1378. controller->slave_abort = pxa2xx_spi_slave_abort;
  1379. controller->handle_err = pxa2xx_spi_handle_err;
  1380. controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1381. controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1382. controller->auto_runtime_pm = true;
  1383. controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
  1384. drv_data->ssp_type = ssp->type;
  1385. if (pxa25x_ssp_comp(drv_data)) {
  1386. switch (drv_data->ssp_type) {
  1387. case QUARK_X1000_SSP:
  1388. controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1389. break;
  1390. default:
  1391. controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1392. break;
  1393. }
  1394. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1395. drv_data->dma_cr1 = 0;
  1396. drv_data->clear_sr = SSSR_ROR;
  1397. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1398. } else {
  1399. controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1400. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1401. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1402. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1403. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
  1404. | SSSR_ROR | SSSR_TUR;
  1405. }
  1406. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1407. drv_data);
  1408. if (status < 0) {
  1409. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1410. goto out_error_controller_alloc;
  1411. }
  1412. /* Setup DMA if requested */
  1413. if (platform_info->enable_dma) {
  1414. status = pxa2xx_spi_dma_setup(drv_data);
  1415. if (status) {
  1416. dev_warn(dev, "no DMA channels available, using PIO\n");
  1417. platform_info->enable_dma = false;
  1418. } else {
  1419. controller->can_dma = pxa2xx_spi_can_dma;
  1420. controller->max_dma_len = MAX_DMA_LEN;
  1421. controller->max_transfer_size =
  1422. pxa2xx_spi_max_dma_transfer_size;
  1423. }
  1424. }
  1425. /* Enable SOC clock */
  1426. status = clk_prepare_enable(ssp->clk);
  1427. if (status)
  1428. goto out_error_dma_irq_alloc;
  1429. controller->max_speed_hz = clk_get_rate(ssp->clk);
  1430. /*
  1431. * Set minimum speed for all other platforms than Intel Quark which is
  1432. * able do under 1 Hz transfers.
  1433. */
  1434. if (!pxa25x_ssp_comp(drv_data))
  1435. controller->min_speed_hz =
  1436. DIV_ROUND_UP(controller->max_speed_hz, 4096);
  1437. else if (!is_quark_x1000_ssp(drv_data))
  1438. controller->min_speed_hz =
  1439. DIV_ROUND_UP(controller->max_speed_hz, 512);
  1440. pxa_ssp_disable(ssp);
  1441. /* Load default SSP configuration */
  1442. switch (drv_data->ssp_type) {
  1443. case QUARK_X1000_SSP:
  1444. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1445. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1446. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1447. /* Using the Motorola SPI protocol and use 8 bit frame */
  1448. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1449. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1450. break;
  1451. case CE4100_SSP:
  1452. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1453. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1454. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1455. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1456. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1457. break;
  1458. default:
  1459. if (spi_controller_is_slave(controller)) {
  1460. tmp = SSCR1_SCFR |
  1461. SSCR1_SCLKDIR |
  1462. SSCR1_SFRMDIR |
  1463. SSCR1_RxTresh(2) |
  1464. SSCR1_TxTresh(1) |
  1465. SSCR1_SPH;
  1466. } else {
  1467. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1468. SSCR1_TxTresh(TX_THRESH_DFLT);
  1469. }
  1470. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1471. tmp = SSCR0_Motorola | SSCR0_DataSize(8);
  1472. if (!spi_controller_is_slave(controller))
  1473. tmp |= SSCR0_SCR(2);
  1474. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1475. break;
  1476. }
  1477. if (!pxa25x_ssp_comp(drv_data))
  1478. pxa2xx_spi_write(drv_data, SSTO, 0);
  1479. if (!is_quark_x1000_ssp(drv_data))
  1480. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1481. if (is_lpss_ssp(drv_data)) {
  1482. lpss_ssp_setup(drv_data);
  1483. config = lpss_get_config(drv_data);
  1484. if (config->reg_capabilities >= 0) {
  1485. tmp = __lpss_ssp_read_priv(drv_data,
  1486. config->reg_capabilities);
  1487. tmp &= LPSS_CAPS_CS_EN_MASK;
  1488. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1489. platform_info->num_chipselect = ffz(tmp);
  1490. } else if (config->cs_num) {
  1491. platform_info->num_chipselect = config->cs_num;
  1492. }
  1493. }
  1494. controller->num_chipselect = platform_info->num_chipselect;
  1495. controller->use_gpio_descriptors = true;
  1496. if (platform_info->is_slave) {
  1497. drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
  1498. "ready", GPIOD_OUT_LOW);
  1499. if (IS_ERR(drv_data->gpiod_ready)) {
  1500. status = PTR_ERR(drv_data->gpiod_ready);
  1501. goto out_error_clock_enabled;
  1502. }
  1503. }
  1504. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1505. pm_runtime_use_autosuspend(&pdev->dev);
  1506. pm_runtime_set_active(&pdev->dev);
  1507. pm_runtime_enable(&pdev->dev);
  1508. /* Register with the SPI framework */
  1509. platform_set_drvdata(pdev, drv_data);
  1510. status = spi_register_controller(controller);
  1511. if (status) {
  1512. dev_err(&pdev->dev, "problem registering SPI controller\n");
  1513. goto out_error_pm_runtime_enabled;
  1514. }
  1515. return status;
  1516. out_error_pm_runtime_enabled:
  1517. pm_runtime_disable(&pdev->dev);
  1518. out_error_clock_enabled:
  1519. clk_disable_unprepare(ssp->clk);
  1520. out_error_dma_irq_alloc:
  1521. pxa2xx_spi_dma_release(drv_data);
  1522. free_irq(ssp->irq, drv_data);
  1523. out_error_controller_alloc:
  1524. pxa_ssp_free(ssp);
  1525. return status;
  1526. }
  1527. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1528. {
  1529. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1530. struct ssp_device *ssp = drv_data->ssp;
  1531. pm_runtime_get_sync(&pdev->dev);
  1532. spi_unregister_controller(drv_data->controller);
  1533. /* Disable the SSP at the peripheral and SOC level */
  1534. pxa_ssp_disable(ssp);
  1535. clk_disable_unprepare(ssp->clk);
  1536. /* Release DMA */
  1537. if (drv_data->controller_info->enable_dma)
  1538. pxa2xx_spi_dma_release(drv_data);
  1539. pm_runtime_put_noidle(&pdev->dev);
  1540. pm_runtime_disable(&pdev->dev);
  1541. /* Release IRQ */
  1542. free_irq(ssp->irq, drv_data);
  1543. /* Release SSP */
  1544. pxa_ssp_free(ssp);
  1545. return 0;
  1546. }
  1547. #ifdef CONFIG_PM_SLEEP
  1548. static int pxa2xx_spi_suspend(struct device *dev)
  1549. {
  1550. struct driver_data *drv_data = dev_get_drvdata(dev);
  1551. struct ssp_device *ssp = drv_data->ssp;
  1552. int status;
  1553. status = spi_controller_suspend(drv_data->controller);
  1554. if (status)
  1555. return status;
  1556. pxa_ssp_disable(ssp);
  1557. if (!pm_runtime_suspended(dev))
  1558. clk_disable_unprepare(ssp->clk);
  1559. return 0;
  1560. }
  1561. static int pxa2xx_spi_resume(struct device *dev)
  1562. {
  1563. struct driver_data *drv_data = dev_get_drvdata(dev);
  1564. struct ssp_device *ssp = drv_data->ssp;
  1565. int status;
  1566. /* Enable the SSP clock */
  1567. if (!pm_runtime_suspended(dev)) {
  1568. status = clk_prepare_enable(ssp->clk);
  1569. if (status)
  1570. return status;
  1571. }
  1572. /* Start the queue running */
  1573. return spi_controller_resume(drv_data->controller);
  1574. }
  1575. #endif
  1576. #ifdef CONFIG_PM
  1577. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1578. {
  1579. struct driver_data *drv_data = dev_get_drvdata(dev);
  1580. clk_disable_unprepare(drv_data->ssp->clk);
  1581. return 0;
  1582. }
  1583. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1584. {
  1585. struct driver_data *drv_data = dev_get_drvdata(dev);
  1586. return clk_prepare_enable(drv_data->ssp->clk);
  1587. }
  1588. #endif
  1589. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1590. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1591. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1592. pxa2xx_spi_runtime_resume, NULL)
  1593. };
  1594. static struct platform_driver driver = {
  1595. .driver = {
  1596. .name = "pxa2xx-spi",
  1597. .pm = &pxa2xx_spi_pm_ops,
  1598. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1599. .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
  1600. },
  1601. .probe = pxa2xx_spi_probe,
  1602. .remove = pxa2xx_spi_remove,
  1603. };
  1604. static int __init pxa2xx_spi_init(void)
  1605. {
  1606. return platform_driver_register(&driver);
  1607. }
  1608. subsys_initcall(pxa2xx_spi_init);
  1609. static void __exit pxa2xx_spi_exit(void)
  1610. {
  1611. platform_driver_unregister(&driver);
  1612. }
  1613. module_exit(pxa2xx_spi_exit);
  1614. MODULE_SOFTDEP("pre: dw_dmac");