fman_port.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*
  2. * Copyright 2008 - 2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include "fman_port.h"
  34. #include "fman.h"
  35. #include "fman_sp.h"
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/of_address.h>
  42. #include <linux/delay.h>
  43. #include <linux/libfdt_env.h>
  44. /* Queue ID */
  45. #define DFLT_FQ_ID 0x00FFFFFF
  46. /* General defines */
  47. #define PORT_BMI_FIFO_UNITS 0x100
  48. #define MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) \
  49. min((u32)bmi_max_fifo_size, (u32)1024 * FMAN_BMI_FIFO_UNITS)
  50. #define PORT_CG_MAP_NUM 8
  51. #define PORT_PRS_RESULT_WORDS_NUM 8
  52. #define PORT_IC_OFFSET_UNITS 0x10
  53. #define MIN_EXT_BUF_SIZE 64
  54. #define BMI_PORT_REGS_OFFSET 0
  55. #define QMI_PORT_REGS_OFFSET 0x400
  56. /* Default values */
  57. #define DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN \
  58. DFLT_FM_SP_BUFFER_PREFIX_CONTEXT_DATA_ALIGN
  59. #define DFLT_PORT_CUT_BYTES_FROM_END 4
  60. #define DFLT_PORT_ERRORS_TO_DISCARD FM_PORT_FRM_ERR_CLS_DISCARD
  61. #define DFLT_PORT_MAX_FRAME_LENGTH 9600
  62. #define DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(bmi_max_fifo_size) \
  63. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size)
  64. #define DFLT_PORT_RX_FIFO_THRESHOLD(major, bmi_max_fifo_size) \
  65. (major == 6 ? \
  66. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) : \
  67. (MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) * 3 / 4)) \
  68. #define DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS 0
  69. /* QMI defines */
  70. #define QMI_DEQ_CFG_SUBPORTAL_MASK 0x1f
  71. #define QMI_PORT_CFG_EN 0x80000000
  72. #define QMI_PORT_STATUS_DEQ_FD_BSY 0x20000000
  73. #define QMI_DEQ_CFG_PRI 0x80000000
  74. #define QMI_DEQ_CFG_TYPE1 0x10000000
  75. #define QMI_DEQ_CFG_TYPE2 0x20000000
  76. #define QMI_DEQ_CFG_TYPE3 0x30000000
  77. #define QMI_DEQ_CFG_PREFETCH_PARTIAL 0x01000000
  78. #define QMI_DEQ_CFG_PREFETCH_FULL 0x03000000
  79. #define QMI_DEQ_CFG_SP_MASK 0xf
  80. #define QMI_DEQ_CFG_SP_SHIFT 20
  81. #define QMI_BYTE_COUNT_LEVEL_CONTROL(_type) \
  82. (_type == FMAN_PORT_TYPE_TX ? 0x1400 : 0x400)
  83. /* BMI defins */
  84. #define BMI_EBD_EN 0x80000000
  85. #define BMI_PORT_CFG_EN 0x80000000
  86. #define BMI_PORT_STATUS_BSY 0x80000000
  87. #define BMI_DMA_ATTR_SWP_SHIFT FMAN_SP_DMA_ATTR_SWP_SHIFT
  88. #define BMI_DMA_ATTR_WRITE_OPTIMIZE FMAN_SP_DMA_ATTR_WRITE_OPTIMIZE
  89. #define BMI_RX_FIFO_PRI_ELEVATION_SHIFT 16
  90. #define BMI_RX_FIFO_THRESHOLD_ETHE 0x80000000
  91. #define BMI_FRAME_END_CS_IGNORE_SHIFT 24
  92. #define BMI_FRAME_END_CS_IGNORE_MASK 0x0000001f
  93. #define BMI_RX_FRAME_END_CUT_SHIFT 16
  94. #define BMI_RX_FRAME_END_CUT_MASK 0x0000001f
  95. #define BMI_IC_TO_EXT_SHIFT FMAN_SP_IC_TO_EXT_SHIFT
  96. #define BMI_IC_TO_EXT_MASK 0x0000001f
  97. #define BMI_IC_FROM_INT_SHIFT FMAN_SP_IC_FROM_INT_SHIFT
  98. #define BMI_IC_FROM_INT_MASK 0x0000000f
  99. #define BMI_IC_SIZE_MASK 0x0000001f
  100. #define BMI_INT_BUF_MARG_SHIFT 28
  101. #define BMI_INT_BUF_MARG_MASK 0x0000000f
  102. #define BMI_EXT_BUF_MARG_START_SHIFT FMAN_SP_EXT_BUF_MARG_START_SHIFT
  103. #define BMI_EXT_BUF_MARG_START_MASK 0x000001ff
  104. #define BMI_EXT_BUF_MARG_END_MASK 0x000001ff
  105. #define BMI_CMD_MR_LEAC 0x00200000
  106. #define BMI_CMD_MR_SLEAC 0x00100000
  107. #define BMI_CMD_MR_MA 0x00080000
  108. #define BMI_CMD_MR_DEAS 0x00040000
  109. #define BMI_CMD_RX_MR_DEF (BMI_CMD_MR_LEAC | \
  110. BMI_CMD_MR_SLEAC | \
  111. BMI_CMD_MR_MA | \
  112. BMI_CMD_MR_DEAS)
  113. #define BMI_CMD_TX_MR_DEF 0
  114. #define BMI_CMD_ATTR_ORDER 0x80000000
  115. #define BMI_CMD_ATTR_SYNC 0x02000000
  116. #define BMI_CMD_ATTR_COLOR_SHIFT 26
  117. #define BMI_FIFO_PIPELINE_DEPTH_SHIFT 12
  118. #define BMI_FIFO_PIPELINE_DEPTH_MASK 0x0000000f
  119. #define BMI_NEXT_ENG_FD_BITS_SHIFT 24
  120. #define BMI_EXT_BUF_POOL_VALID FMAN_SP_EXT_BUF_POOL_VALID
  121. #define BMI_EXT_BUF_POOL_EN_COUNTER FMAN_SP_EXT_BUF_POOL_EN_COUNTER
  122. #define BMI_EXT_BUF_POOL_BACKUP FMAN_SP_EXT_BUF_POOL_BACKUP
  123. #define BMI_EXT_BUF_POOL_ID_SHIFT 16
  124. #define BMI_EXT_BUF_POOL_ID_MASK 0x003F0000
  125. #define BMI_POOL_DEP_NUM_OF_POOLS_SHIFT 16
  126. #define BMI_TX_FIFO_MIN_FILL_SHIFT 16
  127. #define BMI_PRIORITY_ELEVATION_LEVEL ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  128. #define BMI_FIFO_THRESHOLD ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  129. #define BMI_DEQUEUE_PIPELINE_DEPTH(_type, _speed) \
  130. ((_type == FMAN_PORT_TYPE_TX && _speed == 10000) ? 4 : 1)
  131. #define RX_ERRS_TO_ENQ \
  132. (FM_PORT_FRM_ERR_DMA | \
  133. FM_PORT_FRM_ERR_PHYSICAL | \
  134. FM_PORT_FRM_ERR_SIZE | \
  135. FM_PORT_FRM_ERR_EXTRACTION | \
  136. FM_PORT_FRM_ERR_NO_SCHEME | \
  137. FM_PORT_FRM_ERR_PRS_TIMEOUT | \
  138. FM_PORT_FRM_ERR_PRS_ILL_INSTRUCT | \
  139. FM_PORT_FRM_ERR_BLOCK_LIMIT_EXCEEDED | \
  140. FM_PORT_FRM_ERR_PRS_HDR_ERR | \
  141. FM_PORT_FRM_ERR_KEYSIZE_OVERFLOW | \
  142. FM_PORT_FRM_ERR_IPRE)
  143. /* NIA defines */
  144. #define NIA_ORDER_RESTOR 0x00800000
  145. #define NIA_ENG_BMI 0x00500000
  146. #define NIA_ENG_QMI_ENQ 0x00540000
  147. #define NIA_ENG_QMI_DEQ 0x00580000
  148. #define NIA_BMI_AC_ENQ_FRAME 0x00000002
  149. #define NIA_BMI_AC_TX_RELEASE 0x000002C0
  150. #define NIA_BMI_AC_RELEASE 0x000000C0
  151. #define NIA_BMI_AC_TX 0x00000274
  152. #define NIA_BMI_AC_FETCH_ALL_FRAME 0x0000020c
  153. /* Port IDs */
  154. #define TX_10G_PORT_BASE 0x30
  155. #define RX_10G_PORT_BASE 0x10
  156. /* BMI Rx port register map */
  157. struct fman_port_rx_bmi_regs {
  158. u32 fmbm_rcfg; /* Rx Configuration */
  159. u32 fmbm_rst; /* Rx Status */
  160. u32 fmbm_rda; /* Rx DMA attributes */
  161. u32 fmbm_rfp; /* Rx FIFO Parameters */
  162. u32 fmbm_rfed; /* Rx Frame End Data */
  163. u32 fmbm_ricp; /* Rx Internal Context Parameters */
  164. u32 fmbm_rim; /* Rx Internal Buffer Margins */
  165. u32 fmbm_rebm; /* Rx External Buffer Margins */
  166. u32 fmbm_rfne; /* Rx Frame Next Engine */
  167. u32 fmbm_rfca; /* Rx Frame Command Attributes. */
  168. u32 fmbm_rfpne; /* Rx Frame Parser Next Engine */
  169. u32 fmbm_rpso; /* Rx Parse Start Offset */
  170. u32 fmbm_rpp; /* Rx Policer Profile */
  171. u32 fmbm_rccb; /* Rx Coarse Classification Base */
  172. u32 fmbm_reth; /* Rx Excessive Threshold */
  173. u32 reserved003c[1]; /* (0x03C 0x03F) */
  174. u32 fmbm_rprai[PORT_PRS_RESULT_WORDS_NUM];
  175. /* Rx Parse Results Array Init */
  176. u32 fmbm_rfqid; /* Rx Frame Queue ID */
  177. u32 fmbm_refqid; /* Rx Error Frame Queue ID */
  178. u32 fmbm_rfsdm; /* Rx Frame Status Discard Mask */
  179. u32 fmbm_rfsem; /* Rx Frame Status Error Mask */
  180. u32 fmbm_rfene; /* Rx Frame Enqueue Next Engine */
  181. u32 reserved0074[0x2]; /* (0x074-0x07C) */
  182. u32 fmbm_rcmne; /* Rx Frame Continuous Mode Next Engine */
  183. u32 reserved0080[0x20]; /* (0x080 0x0FF) */
  184. u32 fmbm_ebmpi[FMAN_PORT_MAX_EXT_POOLS_NUM];
  185. /* Buffer Manager pool Information- */
  186. u32 fmbm_acnt[FMAN_PORT_MAX_EXT_POOLS_NUM]; /* Allocate Counter- */
  187. u32 reserved0130[8]; /* 0x130/0x140 - 0x15F reserved - */
  188. u32 fmbm_rcgm[PORT_CG_MAP_NUM]; /* Congestion Group Map */
  189. u32 fmbm_mpd; /* BM Pool Depletion */
  190. u32 reserved0184[0x1F]; /* (0x184 0x1FF) */
  191. u32 fmbm_rstc; /* Rx Statistics Counters */
  192. u32 fmbm_rfrc; /* Rx Frame Counter */
  193. u32 fmbm_rfbc; /* Rx Bad Frames Counter */
  194. u32 fmbm_rlfc; /* Rx Large Frames Counter */
  195. u32 fmbm_rffc; /* Rx Filter Frames Counter */
  196. u32 fmbm_rfdc; /* Rx Frame Discard Counter */
  197. u32 fmbm_rfldec; /* Rx Frames List DMA Error Counter */
  198. u32 fmbm_rodc; /* Rx Out of Buffers Discard nntr */
  199. u32 fmbm_rbdc; /* Rx Buffers Deallocate Counter */
  200. u32 fmbm_rpec; /* RX Prepare to enqueue Counte */
  201. u32 reserved0224[0x16]; /* (0x224 0x27F) */
  202. u32 fmbm_rpc; /* Rx Performance Counters */
  203. u32 fmbm_rpcp; /* Rx Performance Count Parameters */
  204. u32 fmbm_rccn; /* Rx Cycle Counter */
  205. u32 fmbm_rtuc; /* Rx Tasks Utilization Counter */
  206. u32 fmbm_rrquc; /* Rx Receive Queue Utilization cntr */
  207. u32 fmbm_rduc; /* Rx DMA Utilization Counter */
  208. u32 fmbm_rfuc; /* Rx FIFO Utilization Counter */
  209. u32 fmbm_rpac; /* Rx Pause Activation Counter */
  210. u32 reserved02a0[0x18]; /* (0x2A0 0x2FF) */
  211. u32 fmbm_rdcfg[0x3]; /* Rx Debug Configuration */
  212. u32 fmbm_rgpr; /* Rx General Purpose Register */
  213. u32 reserved0310[0x3a];
  214. };
  215. /* BMI Tx port register map */
  216. struct fman_port_tx_bmi_regs {
  217. u32 fmbm_tcfg; /* Tx Configuration */
  218. u32 fmbm_tst; /* Tx Status */
  219. u32 fmbm_tda; /* Tx DMA attributes */
  220. u32 fmbm_tfp; /* Tx FIFO Parameters */
  221. u32 fmbm_tfed; /* Tx Frame End Data */
  222. u32 fmbm_ticp; /* Tx Internal Context Parameters */
  223. u32 fmbm_tfdne; /* Tx Frame Dequeue Next Engine. */
  224. u32 fmbm_tfca; /* Tx Frame Command attribute. */
  225. u32 fmbm_tcfqid; /* Tx Confirmation Frame Queue ID. */
  226. u32 fmbm_tefqid; /* Tx Frame Error Queue ID */
  227. u32 fmbm_tfene; /* Tx Frame Enqueue Next Engine */
  228. u32 fmbm_trlmts; /* Tx Rate Limiter Scale */
  229. u32 fmbm_trlmt; /* Tx Rate Limiter */
  230. u32 reserved0034[0x0e]; /* (0x034-0x6c) */
  231. u32 fmbm_tccb; /* Tx Coarse Classification base */
  232. u32 fmbm_tfne; /* Tx Frame Next Engine */
  233. u32 fmbm_tpfcm[0x02];
  234. /* Tx Priority based Flow Control (PFC) Mapping */
  235. u32 fmbm_tcmne; /* Tx Frame Continuous Mode Next Engine */
  236. u32 reserved0080[0x60]; /* (0x080-0x200) */
  237. u32 fmbm_tstc; /* Tx Statistics Counters */
  238. u32 fmbm_tfrc; /* Tx Frame Counter */
  239. u32 fmbm_tfdc; /* Tx Frames Discard Counter */
  240. u32 fmbm_tfledc; /* Tx Frame len error discard cntr */
  241. u32 fmbm_tfufdc; /* Tx Frame unsprt frmt discard cntr */
  242. u32 fmbm_tbdc; /* Tx Buffers Deallocate Counter */
  243. u32 reserved0218[0x1A]; /* (0x218-0x280) */
  244. u32 fmbm_tpc; /* Tx Performance Counters */
  245. u32 fmbm_tpcp; /* Tx Performance Count Parameters */
  246. u32 fmbm_tccn; /* Tx Cycle Counter */
  247. u32 fmbm_ttuc; /* Tx Tasks Utilization Counter */
  248. u32 fmbm_ttcquc; /* Tx Transmit conf Q util Counter */
  249. u32 fmbm_tduc; /* Tx DMA Utilization Counter */
  250. u32 fmbm_tfuc; /* Tx FIFO Utilization Counter */
  251. u32 reserved029c[16]; /* (0x29C-0x2FF) */
  252. u32 fmbm_tdcfg[0x3]; /* Tx Debug Configuration */
  253. u32 fmbm_tgpr; /* Tx General Purpose Register */
  254. u32 reserved0310[0x3a]; /* (0x310-0x3FF) */
  255. };
  256. /* BMI port register map */
  257. union fman_port_bmi_regs {
  258. struct fman_port_rx_bmi_regs rx;
  259. struct fman_port_tx_bmi_regs tx;
  260. };
  261. /* QMI port register map */
  262. struct fman_port_qmi_regs {
  263. u32 fmqm_pnc; /* PortID n Configuration Register */
  264. u32 fmqm_pns; /* PortID n Status Register */
  265. u32 fmqm_pnts; /* PortID n Task Status Register */
  266. u32 reserved00c[4]; /* 0xn00C - 0xn01B */
  267. u32 fmqm_pnen; /* PortID n Enqueue NIA Register */
  268. u32 fmqm_pnetfc; /* PortID n Enq Total Frame Counter */
  269. u32 reserved024[2]; /* 0xn024 - 0x02B */
  270. u32 fmqm_pndn; /* PortID n Dequeue NIA Register */
  271. u32 fmqm_pndc; /* PortID n Dequeue Config Register */
  272. u32 fmqm_pndtfc; /* PortID n Dequeue tot Frame cntr */
  273. u32 fmqm_pndfdc; /* PortID n Dequeue FQID Dflt Cntr */
  274. u32 fmqm_pndcc; /* PortID n Dequeue Confirm Counter */
  275. };
  276. /* QMI dequeue prefetch modes */
  277. enum fman_port_deq_prefetch {
  278. FMAN_PORT_DEQ_NO_PREFETCH, /* No prefetch mode */
  279. FMAN_PORT_DEQ_PART_PREFETCH, /* Partial prefetch mode */
  280. FMAN_PORT_DEQ_FULL_PREFETCH /* Full prefetch mode */
  281. };
  282. /* A structure for defining FM port resources */
  283. struct fman_port_rsrc {
  284. u32 num; /* Committed required resource */
  285. u32 extra; /* Extra (not committed) required resource */
  286. };
  287. enum fman_port_dma_swap {
  288. FMAN_PORT_DMA_NO_SWAP, /* No swap, transfer data as is */
  289. FMAN_PORT_DMA_SWAP_LE,
  290. /* The transferred data should be swapped in PPC Little Endian mode */
  291. FMAN_PORT_DMA_SWAP_BE
  292. /* The transferred data should be swapped in Big Endian mode */
  293. };
  294. /* Default port color */
  295. enum fman_port_color {
  296. FMAN_PORT_COLOR_GREEN, /* Default port color is green */
  297. FMAN_PORT_COLOR_YELLOW, /* Default port color is yellow */
  298. FMAN_PORT_COLOR_RED, /* Default port color is red */
  299. FMAN_PORT_COLOR_OVERRIDE /* Ignore color */
  300. };
  301. /* QMI dequeue from the SP channel - types */
  302. enum fman_port_deq_type {
  303. FMAN_PORT_DEQ_BY_PRI,
  304. /* Priority precedence and Intra-Class scheduling */
  305. FMAN_PORT_DEQ_ACTIVE_FQ,
  306. /* Active FQ precedence and Intra-Class scheduling */
  307. FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS
  308. /* Active FQ precedence and override Intra-Class scheduling */
  309. };
  310. /* External buffer pools configuration */
  311. struct fman_port_bpools {
  312. u8 count; /* Num of pools to set up */
  313. bool counters_enable; /* Enable allocate counters */
  314. u8 grp_bp_depleted_num;
  315. /* Number of depleted pools - if reached the BMI indicates
  316. * the MAC to send a pause frame
  317. */
  318. struct {
  319. u8 bpid; /* BM pool ID */
  320. u16 size;
  321. /* Pool's size - must be in ascending order */
  322. bool is_backup;
  323. /* If this is a backup pool */
  324. bool grp_bp_depleted;
  325. /* Consider this buffer in multiple pools depletion criteria */
  326. bool single_bp_depleted;
  327. /* Consider this buffer in single pool depletion criteria */
  328. } bpool[FMAN_PORT_MAX_EXT_POOLS_NUM];
  329. };
  330. struct fman_port_cfg {
  331. u32 dflt_fqid;
  332. u32 err_fqid;
  333. u8 deq_sp;
  334. bool deq_high_priority;
  335. enum fman_port_deq_type deq_type;
  336. enum fman_port_deq_prefetch deq_prefetch_option;
  337. u16 deq_byte_cnt;
  338. u8 cheksum_last_bytes_ignore;
  339. u8 rx_cut_end_bytes;
  340. struct fman_buf_pool_depletion buf_pool_depletion;
  341. struct fman_ext_pools ext_buf_pools;
  342. u32 tx_fifo_min_level;
  343. u32 tx_fifo_low_comf_level;
  344. u32 rx_pri_elevation;
  345. u32 rx_fifo_thr;
  346. struct fman_sp_buf_margins buf_margins;
  347. u32 int_buf_start_margin;
  348. struct fman_sp_int_context_data_copy int_context;
  349. u32 discard_mask;
  350. u32 err_mask;
  351. struct fman_buffer_prefix_content buffer_prefix_content;
  352. bool dont_release_buf;
  353. u8 rx_fd_bits;
  354. u32 tx_fifo_deq_pipeline_depth;
  355. bool errata_A006320;
  356. bool excessive_threshold_register;
  357. bool fmbm_tfne_has_features;
  358. enum fman_port_dma_swap dma_swap_data;
  359. enum fman_port_color color;
  360. };
  361. struct fman_port_rx_pools_params {
  362. u8 num_of_pools;
  363. u16 second_largest_buf_size;
  364. u16 largest_buf_size;
  365. };
  366. struct fman_port_dts_params {
  367. void __iomem *base_addr; /* FMan port virtual memory */
  368. enum fman_port_type type; /* Port type */
  369. u16 speed; /* Port speed */
  370. u8 id; /* HW Port Id */
  371. u32 qman_channel_id; /* QMan channel id (non RX only) */
  372. struct fman *fman; /* FMan Handle */
  373. };
  374. struct fman_port {
  375. void *fm;
  376. struct device *dev;
  377. struct fman_rev_info rev_info;
  378. u8 port_id;
  379. enum fman_port_type port_type;
  380. u16 port_speed;
  381. union fman_port_bmi_regs __iomem *bmi_regs;
  382. struct fman_port_qmi_regs __iomem *qmi_regs;
  383. struct fman_sp_buffer_offsets buffer_offsets;
  384. u8 internal_buf_offset;
  385. struct fman_ext_pools ext_buf_pools;
  386. u16 max_frame_length;
  387. struct fman_port_rsrc open_dmas;
  388. struct fman_port_rsrc tasks;
  389. struct fman_port_rsrc fifo_bufs;
  390. struct fman_port_rx_pools_params rx_pools_params;
  391. struct fman_port_cfg *cfg;
  392. struct fman_port_dts_params dts_params;
  393. u8 ext_pools_num;
  394. u32 max_port_fifo_size;
  395. u32 max_num_of_ext_pools;
  396. u32 max_num_of_sub_portals;
  397. u32 bm_max_num_of_pools;
  398. };
  399. static int init_bmi_rx(struct fman_port *port)
  400. {
  401. struct fman_port_rx_bmi_regs __iomem *regs = &port->bmi_regs->rx;
  402. struct fman_port_cfg *cfg = port->cfg;
  403. u32 tmp;
  404. /* DMA attributes */
  405. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  406. /* Enable write optimization */
  407. tmp |= BMI_DMA_ATTR_WRITE_OPTIMIZE;
  408. iowrite32be(tmp, &regs->fmbm_rda);
  409. /* Rx FIFO parameters */
  410. tmp = (cfg->rx_pri_elevation / PORT_BMI_FIFO_UNITS - 1) <<
  411. BMI_RX_FIFO_PRI_ELEVATION_SHIFT;
  412. tmp |= cfg->rx_fifo_thr / PORT_BMI_FIFO_UNITS - 1;
  413. iowrite32be(tmp, &regs->fmbm_rfp);
  414. if (cfg->excessive_threshold_register)
  415. /* always allow access to the extra resources */
  416. iowrite32be(BMI_RX_FIFO_THRESHOLD_ETHE, &regs->fmbm_reth);
  417. /* Frame end data */
  418. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  419. BMI_FRAME_END_CS_IGNORE_SHIFT;
  420. tmp |= (cfg->rx_cut_end_bytes & BMI_RX_FRAME_END_CUT_MASK) <<
  421. BMI_RX_FRAME_END_CUT_SHIFT;
  422. if (cfg->errata_A006320)
  423. tmp &= 0xffe0ffff;
  424. iowrite32be(tmp, &regs->fmbm_rfed);
  425. /* Internal context parameters */
  426. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  427. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  428. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  429. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  430. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  431. BMI_IC_SIZE_MASK;
  432. iowrite32be(tmp, &regs->fmbm_ricp);
  433. /* Internal buffer offset */
  434. tmp = ((cfg->int_buf_start_margin / PORT_IC_OFFSET_UNITS) &
  435. BMI_INT_BUF_MARG_MASK) << BMI_INT_BUF_MARG_SHIFT;
  436. iowrite32be(tmp, &regs->fmbm_rim);
  437. /* External buffer margins */
  438. tmp = (cfg->buf_margins.start_margins & BMI_EXT_BUF_MARG_START_MASK) <<
  439. BMI_EXT_BUF_MARG_START_SHIFT;
  440. tmp |= cfg->buf_margins.end_margins & BMI_EXT_BUF_MARG_END_MASK;
  441. iowrite32be(tmp, &regs->fmbm_rebm);
  442. /* Frame attributes */
  443. tmp = BMI_CMD_RX_MR_DEF;
  444. tmp |= BMI_CMD_ATTR_ORDER;
  445. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  446. /* Synchronization request */
  447. tmp |= BMI_CMD_ATTR_SYNC;
  448. iowrite32be(tmp, &regs->fmbm_rfca);
  449. /* NIA */
  450. tmp = (u32)cfg->rx_fd_bits << BMI_NEXT_ENG_FD_BITS_SHIFT;
  451. tmp |= NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME;
  452. iowrite32be(tmp, &regs->fmbm_rfne);
  453. /* Enqueue NIA */
  454. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_rfene);
  455. /* Default/error queues */
  456. iowrite32be((cfg->dflt_fqid & DFLT_FQ_ID), &regs->fmbm_rfqid);
  457. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_refqid);
  458. /* Discard/error masks */
  459. iowrite32be(cfg->discard_mask, &regs->fmbm_rfsdm);
  460. iowrite32be(cfg->err_mask, &regs->fmbm_rfsem);
  461. return 0;
  462. }
  463. static int init_bmi_tx(struct fman_port *port)
  464. {
  465. struct fman_port_tx_bmi_regs __iomem *regs = &port->bmi_regs->tx;
  466. struct fman_port_cfg *cfg = port->cfg;
  467. u32 tmp;
  468. /* Tx Configuration register */
  469. tmp = 0;
  470. iowrite32be(tmp, &regs->fmbm_tcfg);
  471. /* DMA attributes */
  472. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  473. iowrite32be(tmp, &regs->fmbm_tda);
  474. /* Tx FIFO parameters */
  475. tmp = (cfg->tx_fifo_min_level / PORT_BMI_FIFO_UNITS) <<
  476. BMI_TX_FIFO_MIN_FILL_SHIFT;
  477. tmp |= ((cfg->tx_fifo_deq_pipeline_depth - 1) &
  478. BMI_FIFO_PIPELINE_DEPTH_MASK) << BMI_FIFO_PIPELINE_DEPTH_SHIFT;
  479. tmp |= (cfg->tx_fifo_low_comf_level / PORT_BMI_FIFO_UNITS) - 1;
  480. iowrite32be(tmp, &regs->fmbm_tfp);
  481. /* Frame end data */
  482. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  483. BMI_FRAME_END_CS_IGNORE_SHIFT;
  484. iowrite32be(tmp, &regs->fmbm_tfed);
  485. /* Internal context parameters */
  486. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  487. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  488. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  489. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  490. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  491. BMI_IC_SIZE_MASK;
  492. iowrite32be(tmp, &regs->fmbm_ticp);
  493. /* Frame attributes */
  494. tmp = BMI_CMD_TX_MR_DEF;
  495. tmp |= BMI_CMD_ATTR_ORDER;
  496. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  497. iowrite32be(tmp, &regs->fmbm_tfca);
  498. /* Dequeue NIA + enqueue NIA */
  499. iowrite32be(NIA_ENG_QMI_DEQ, &regs->fmbm_tfdne);
  500. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_tfene);
  501. if (cfg->fmbm_tfne_has_features)
  502. iowrite32be(!cfg->dflt_fqid ?
  503. BMI_EBD_EN | NIA_BMI_AC_FETCH_ALL_FRAME :
  504. NIA_BMI_AC_FETCH_ALL_FRAME, &regs->fmbm_tfne);
  505. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  506. iowrite32be(DFLT_FQ_ID, &regs->fmbm_tcfqid);
  507. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  508. &regs->fmbm_tfene);
  509. if (cfg->fmbm_tfne_has_features)
  510. iowrite32be(ioread32be(&regs->fmbm_tfne) & ~BMI_EBD_EN,
  511. &regs->fmbm_tfne);
  512. }
  513. /* Confirmation/error queues */
  514. if (cfg->dflt_fqid || !cfg->dont_release_buf)
  515. iowrite32be(cfg->dflt_fqid & DFLT_FQ_ID, &regs->fmbm_tcfqid);
  516. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_tefqid);
  517. return 0;
  518. }
  519. static int init_qmi(struct fman_port *port)
  520. {
  521. struct fman_port_qmi_regs __iomem *regs = port->qmi_regs;
  522. struct fman_port_cfg *cfg = port->cfg;
  523. u32 tmp;
  524. /* Rx port configuration */
  525. if (port->port_type == FMAN_PORT_TYPE_RX) {
  526. /* Enqueue NIA */
  527. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_RELEASE, &regs->fmqm_pnen);
  528. return 0;
  529. }
  530. /* Continue with Tx port configuration */
  531. if (port->port_type == FMAN_PORT_TYPE_TX) {
  532. /* Enqueue NIA */
  533. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  534. &regs->fmqm_pnen);
  535. /* Dequeue NIA */
  536. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX, &regs->fmqm_pndn);
  537. }
  538. /* Dequeue Configuration register */
  539. tmp = 0;
  540. if (cfg->deq_high_priority)
  541. tmp |= QMI_DEQ_CFG_PRI;
  542. switch (cfg->deq_type) {
  543. case FMAN_PORT_DEQ_BY_PRI:
  544. tmp |= QMI_DEQ_CFG_TYPE1;
  545. break;
  546. case FMAN_PORT_DEQ_ACTIVE_FQ:
  547. tmp |= QMI_DEQ_CFG_TYPE2;
  548. break;
  549. case FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS:
  550. tmp |= QMI_DEQ_CFG_TYPE3;
  551. break;
  552. default:
  553. return -EINVAL;
  554. }
  555. switch (cfg->deq_prefetch_option) {
  556. case FMAN_PORT_DEQ_NO_PREFETCH:
  557. break;
  558. case FMAN_PORT_DEQ_PART_PREFETCH:
  559. tmp |= QMI_DEQ_CFG_PREFETCH_PARTIAL;
  560. break;
  561. case FMAN_PORT_DEQ_FULL_PREFETCH:
  562. tmp |= QMI_DEQ_CFG_PREFETCH_FULL;
  563. break;
  564. default:
  565. return -EINVAL;
  566. }
  567. tmp |= (cfg->deq_sp & QMI_DEQ_CFG_SP_MASK) << QMI_DEQ_CFG_SP_SHIFT;
  568. tmp |= cfg->deq_byte_cnt;
  569. iowrite32be(tmp, &regs->fmqm_pndc);
  570. return 0;
  571. }
  572. static int init(struct fman_port *port)
  573. {
  574. int err;
  575. /* Init BMI registers */
  576. switch (port->port_type) {
  577. case FMAN_PORT_TYPE_RX:
  578. err = init_bmi_rx(port);
  579. break;
  580. case FMAN_PORT_TYPE_TX:
  581. err = init_bmi_tx(port);
  582. break;
  583. default:
  584. return -EINVAL;
  585. }
  586. if (err)
  587. return err;
  588. /* Init QMI registers */
  589. err = init_qmi(port);
  590. return err;
  591. return 0;
  592. }
  593. static int set_bpools(const struct fman_port *port,
  594. const struct fman_port_bpools *bp)
  595. {
  596. u32 __iomem *bp_reg, *bp_depl_reg;
  597. u32 tmp;
  598. u8 i, max_bp_num;
  599. bool grp_depl_used = false, rx_port;
  600. switch (port->port_type) {
  601. case FMAN_PORT_TYPE_RX:
  602. max_bp_num = port->ext_pools_num;
  603. rx_port = true;
  604. bp_reg = port->bmi_regs->rx.fmbm_ebmpi;
  605. bp_depl_reg = &port->bmi_regs->rx.fmbm_mpd;
  606. break;
  607. default:
  608. return -EINVAL;
  609. }
  610. if (rx_port) {
  611. /* Check buffers are provided in ascending order */
  612. for (i = 0; (i < (bp->count - 1) &&
  613. (i < FMAN_PORT_MAX_EXT_POOLS_NUM - 1)); i++) {
  614. if (bp->bpool[i].size > bp->bpool[i + 1].size)
  615. return -EINVAL;
  616. }
  617. }
  618. /* Set up external buffers pools */
  619. for (i = 0; i < bp->count; i++) {
  620. tmp = BMI_EXT_BUF_POOL_VALID;
  621. tmp |= ((u32)bp->bpool[i].bpid <<
  622. BMI_EXT_BUF_POOL_ID_SHIFT) & BMI_EXT_BUF_POOL_ID_MASK;
  623. if (rx_port) {
  624. if (bp->counters_enable)
  625. tmp |= BMI_EXT_BUF_POOL_EN_COUNTER;
  626. if (bp->bpool[i].is_backup)
  627. tmp |= BMI_EXT_BUF_POOL_BACKUP;
  628. tmp |= (u32)bp->bpool[i].size;
  629. }
  630. iowrite32be(tmp, &bp_reg[i]);
  631. }
  632. /* Clear unused pools */
  633. for (i = bp->count; i < max_bp_num; i++)
  634. iowrite32be(0, &bp_reg[i]);
  635. /* Pools depletion */
  636. tmp = 0;
  637. for (i = 0; i < FMAN_PORT_MAX_EXT_POOLS_NUM; i++) {
  638. if (bp->bpool[i].grp_bp_depleted) {
  639. grp_depl_used = true;
  640. tmp |= 0x80000000 >> i;
  641. }
  642. if (bp->bpool[i].single_bp_depleted)
  643. tmp |= 0x80 >> i;
  644. }
  645. if (grp_depl_used)
  646. tmp |= ((u32)bp->grp_bp_depleted_num - 1) <<
  647. BMI_POOL_DEP_NUM_OF_POOLS_SHIFT;
  648. iowrite32be(tmp, bp_depl_reg);
  649. return 0;
  650. }
  651. static bool is_init_done(struct fman_port_cfg *cfg)
  652. {
  653. /* Checks if FMan port driver parameters were initialized */
  654. if (!cfg)
  655. return true;
  656. return false;
  657. }
  658. static int verify_size_of_fifo(struct fman_port *port)
  659. {
  660. u32 min_fifo_size_required = 0, opt_fifo_size_for_b2b = 0;
  661. /* TX Ports */
  662. if (port->port_type == FMAN_PORT_TYPE_TX) {
  663. min_fifo_size_required = (u32)
  664. (roundup(port->max_frame_length,
  665. FMAN_BMI_FIFO_UNITS) + (3 * FMAN_BMI_FIFO_UNITS));
  666. min_fifo_size_required +=
  667. port->cfg->tx_fifo_deq_pipeline_depth *
  668. FMAN_BMI_FIFO_UNITS;
  669. opt_fifo_size_for_b2b = min_fifo_size_required;
  670. /* Add some margin for back-to-back capability to improve
  671. * performance, allows the hardware to pipeline new frame dma
  672. * while the previous frame not yet transmitted.
  673. */
  674. if (port->port_speed == 10000)
  675. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  676. else
  677. opt_fifo_size_for_b2b += 2 * FMAN_BMI_FIFO_UNITS;
  678. }
  679. /* RX Ports */
  680. else if (port->port_type == FMAN_PORT_TYPE_RX) {
  681. if (port->rev_info.major >= 6)
  682. min_fifo_size_required = (u32)
  683. (roundup(port->max_frame_length,
  684. FMAN_BMI_FIFO_UNITS) +
  685. (5 * FMAN_BMI_FIFO_UNITS));
  686. /* 4 according to spec + 1 for FOF>0 */
  687. else
  688. min_fifo_size_required = (u32)
  689. (roundup(min(port->max_frame_length,
  690. port->rx_pools_params.largest_buf_size),
  691. FMAN_BMI_FIFO_UNITS) +
  692. (7 * FMAN_BMI_FIFO_UNITS));
  693. opt_fifo_size_for_b2b = min_fifo_size_required;
  694. /* Add some margin for back-to-back capability to improve
  695. * performance,allows the hardware to pipeline new frame dma
  696. * while the previous frame not yet transmitted.
  697. */
  698. if (port->port_speed == 10000)
  699. opt_fifo_size_for_b2b += 8 * FMAN_BMI_FIFO_UNITS;
  700. else
  701. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  702. }
  703. WARN_ON(min_fifo_size_required <= 0);
  704. WARN_ON(opt_fifo_size_for_b2b < min_fifo_size_required);
  705. /* Verify the size */
  706. if (port->fifo_bufs.num < min_fifo_size_required)
  707. dev_dbg(port->dev, "%s: FIFO size should be enlarged to %d bytes\n",
  708. __func__, min_fifo_size_required);
  709. else if (port->fifo_bufs.num < opt_fifo_size_for_b2b)
  710. dev_dbg(port->dev, "%s: For b2b processing,FIFO may be enlarged to %d bytes\n",
  711. __func__, opt_fifo_size_for_b2b);
  712. return 0;
  713. }
  714. static int set_ext_buffer_pools(struct fman_port *port)
  715. {
  716. struct fman_ext_pools *ext_buf_pools = &port->cfg->ext_buf_pools;
  717. struct fman_buf_pool_depletion *buf_pool_depletion =
  718. &port->cfg->buf_pool_depletion;
  719. u8 ordered_array[FMAN_PORT_MAX_EXT_POOLS_NUM];
  720. u16 sizes_array[BM_MAX_NUM_OF_POOLS];
  721. int i = 0, j = 0, err;
  722. struct fman_port_bpools bpools;
  723. memset(&ordered_array, 0, sizeof(u8) * FMAN_PORT_MAX_EXT_POOLS_NUM);
  724. memset(&sizes_array, 0, sizeof(u16) * BM_MAX_NUM_OF_POOLS);
  725. memcpy(&port->ext_buf_pools, ext_buf_pools,
  726. sizeof(struct fman_ext_pools));
  727. fman_sp_set_buf_pools_in_asc_order_of_buf_sizes(ext_buf_pools,
  728. ordered_array,
  729. sizes_array);
  730. memset(&bpools, 0, sizeof(struct fman_port_bpools));
  731. bpools.count = ext_buf_pools->num_of_pools_used;
  732. bpools.counters_enable = true;
  733. for (i = 0; i < ext_buf_pools->num_of_pools_used; i++) {
  734. bpools.bpool[i].bpid = ordered_array[i];
  735. bpools.bpool[i].size = sizes_array[ordered_array[i]];
  736. }
  737. /* save pools parameters for later use */
  738. port->rx_pools_params.num_of_pools = ext_buf_pools->num_of_pools_used;
  739. port->rx_pools_params.largest_buf_size =
  740. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 1]];
  741. port->rx_pools_params.second_largest_buf_size =
  742. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 2]];
  743. /* FMBM_RMPD reg. - pool depletion */
  744. if (buf_pool_depletion->pools_grp_mode_enable) {
  745. bpools.grp_bp_depleted_num = buf_pool_depletion->num_of_pools;
  746. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  747. if (buf_pool_depletion->pools_to_consider[i]) {
  748. for (j = 0; j < ext_buf_pools->
  749. num_of_pools_used; j++) {
  750. if (i == ordered_array[j]) {
  751. bpools.bpool[j].
  752. grp_bp_depleted = true;
  753. break;
  754. }
  755. }
  756. }
  757. }
  758. }
  759. if (buf_pool_depletion->single_pool_mode_enable) {
  760. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  761. if (buf_pool_depletion->
  762. pools_to_consider_for_single_mode[i]) {
  763. for (j = 0; j < ext_buf_pools->
  764. num_of_pools_used; j++) {
  765. if (i == ordered_array[j]) {
  766. bpools.bpool[j].
  767. single_bp_depleted = true;
  768. break;
  769. }
  770. }
  771. }
  772. }
  773. }
  774. err = set_bpools(port, &bpools);
  775. if (err != 0) {
  776. dev_err(port->dev, "%s: set_bpools() failed\n", __func__);
  777. return -EINVAL;
  778. }
  779. return 0;
  780. }
  781. static int init_low_level_driver(struct fman_port *port)
  782. {
  783. struct fman_port_cfg *cfg = port->cfg;
  784. u32 tmp_val;
  785. switch (port->port_type) {
  786. case FMAN_PORT_TYPE_RX:
  787. cfg->err_mask = (RX_ERRS_TO_ENQ & ~cfg->discard_mask);
  788. break;
  789. default:
  790. break;
  791. }
  792. tmp_val = (u32)((port->internal_buf_offset % OFFSET_UNITS) ?
  793. (port->internal_buf_offset / OFFSET_UNITS + 1) :
  794. (port->internal_buf_offset / OFFSET_UNITS));
  795. port->internal_buf_offset = (u8)(tmp_val * OFFSET_UNITS);
  796. port->cfg->int_buf_start_margin = port->internal_buf_offset;
  797. if (init(port) != 0) {
  798. dev_err(port->dev, "%s: fman port initialization failed\n",
  799. __func__);
  800. return -ENODEV;
  801. }
  802. /* The code bellow is a trick so the FM will not release the buffer
  803. * to BM nor will try to enqueue the frame to QM
  804. */
  805. if (port->port_type == FMAN_PORT_TYPE_TX) {
  806. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  807. /* override fmbm_tcfqid 0 with a false non-0 value.
  808. * This will force FM to act according to tfene.
  809. * Otherwise, if fmbm_tcfqid is 0 the FM will release
  810. * buffers to BM regardless of fmbm_tfene
  811. */
  812. iowrite32be(0xFFFFFF, &port->bmi_regs->tx.fmbm_tcfqid);
  813. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  814. &port->bmi_regs->tx.fmbm_tfene);
  815. }
  816. }
  817. return 0;
  818. }
  819. static int fill_soc_specific_params(struct fman_port *port)
  820. {
  821. u32 bmi_max_fifo_size;
  822. bmi_max_fifo_size = fman_get_bmi_max_fifo_size(port->fm);
  823. port->max_port_fifo_size = MAX_PORT_FIFO_SIZE(bmi_max_fifo_size);
  824. port->bm_max_num_of_pools = 64;
  825. /* P4080 - Major 2
  826. * P2041/P3041/P5020/P5040 - Major 3
  827. * Tx/Bx - Major 6
  828. */
  829. switch (port->rev_info.major) {
  830. case 2:
  831. case 3:
  832. port->max_num_of_ext_pools = 4;
  833. port->max_num_of_sub_portals = 12;
  834. break;
  835. case 6:
  836. port->max_num_of_ext_pools = 8;
  837. port->max_num_of_sub_portals = 16;
  838. break;
  839. default:
  840. dev_err(port->dev, "%s: Unsupported FMan version\n", __func__);
  841. return -EINVAL;
  842. }
  843. return 0;
  844. }
  845. static int get_dflt_fifo_deq_pipeline_depth(u8 major, enum fman_port_type type,
  846. u16 speed)
  847. {
  848. switch (type) {
  849. case FMAN_PORT_TYPE_RX:
  850. case FMAN_PORT_TYPE_TX:
  851. switch (speed) {
  852. case 10000:
  853. return 4;
  854. case 1000:
  855. if (major >= 6)
  856. return 2;
  857. else
  858. return 1;
  859. default:
  860. return 0;
  861. }
  862. default:
  863. return 0;
  864. }
  865. }
  866. static int get_dflt_num_of_tasks(u8 major, enum fman_port_type type,
  867. u16 speed)
  868. {
  869. switch (type) {
  870. case FMAN_PORT_TYPE_RX:
  871. case FMAN_PORT_TYPE_TX:
  872. switch (speed) {
  873. case 10000:
  874. return 16;
  875. case 1000:
  876. if (major >= 6)
  877. return 4;
  878. else
  879. return 3;
  880. default:
  881. return 0;
  882. }
  883. default:
  884. return 0;
  885. }
  886. }
  887. static int get_dflt_extra_num_of_tasks(u8 major, enum fman_port_type type,
  888. u16 speed)
  889. {
  890. switch (type) {
  891. case FMAN_PORT_TYPE_RX:
  892. /* FMan V3 */
  893. if (major >= 6)
  894. return 0;
  895. /* FMan V2 */
  896. if (speed == 10000)
  897. return 8;
  898. else
  899. return 2;
  900. case FMAN_PORT_TYPE_TX:
  901. default:
  902. return 0;
  903. }
  904. }
  905. static int get_dflt_num_of_open_dmas(u8 major, enum fman_port_type type,
  906. u16 speed)
  907. {
  908. int val;
  909. if (major >= 6) {
  910. switch (type) {
  911. case FMAN_PORT_TYPE_TX:
  912. if (speed == 10000)
  913. val = 12;
  914. else
  915. val = 3;
  916. break;
  917. case FMAN_PORT_TYPE_RX:
  918. if (speed == 10000)
  919. val = 8;
  920. else
  921. val = 2;
  922. break;
  923. default:
  924. return 0;
  925. }
  926. } else {
  927. switch (type) {
  928. case FMAN_PORT_TYPE_TX:
  929. case FMAN_PORT_TYPE_RX:
  930. if (speed == 10000)
  931. val = 8;
  932. else
  933. val = 1;
  934. break;
  935. default:
  936. val = 0;
  937. }
  938. }
  939. return val;
  940. }
  941. static int get_dflt_extra_num_of_open_dmas(u8 major, enum fman_port_type type,
  942. u16 speed)
  943. {
  944. /* FMan V3 */
  945. if (major >= 6)
  946. return 0;
  947. /* FMan V2 */
  948. switch (type) {
  949. case FMAN_PORT_TYPE_RX:
  950. case FMAN_PORT_TYPE_TX:
  951. if (speed == 10000)
  952. return 8;
  953. else
  954. return 1;
  955. default:
  956. return 0;
  957. }
  958. }
  959. static int get_dflt_num_of_fifo_bufs(u8 major, enum fman_port_type type,
  960. u16 speed)
  961. {
  962. int val;
  963. if (major >= 6) {
  964. switch (type) {
  965. case FMAN_PORT_TYPE_TX:
  966. if (speed == 10000)
  967. val = 64;
  968. else
  969. val = 50;
  970. break;
  971. case FMAN_PORT_TYPE_RX:
  972. if (speed == 10000)
  973. val = 96;
  974. else
  975. val = 50;
  976. break;
  977. default:
  978. val = 0;
  979. }
  980. } else {
  981. switch (type) {
  982. case FMAN_PORT_TYPE_TX:
  983. if (speed == 10000)
  984. val = 48;
  985. else
  986. val = 44;
  987. break;
  988. case FMAN_PORT_TYPE_RX:
  989. if (speed == 10000)
  990. val = 48;
  991. else
  992. val = 45;
  993. break;
  994. default:
  995. val = 0;
  996. }
  997. }
  998. return val;
  999. }
  1000. static void set_dflt_cfg(struct fman_port *port,
  1001. struct fman_port_params *port_params)
  1002. {
  1003. struct fman_port_cfg *cfg = port->cfg;
  1004. cfg->dma_swap_data = FMAN_PORT_DMA_NO_SWAP;
  1005. cfg->color = FMAN_PORT_COLOR_GREEN;
  1006. cfg->rx_cut_end_bytes = DFLT_PORT_CUT_BYTES_FROM_END;
  1007. cfg->rx_pri_elevation = BMI_PRIORITY_ELEVATION_LEVEL;
  1008. cfg->rx_fifo_thr = BMI_FIFO_THRESHOLD;
  1009. cfg->tx_fifo_low_comf_level = (5 * 1024);
  1010. cfg->deq_type = FMAN_PORT_DEQ_BY_PRI;
  1011. cfg->deq_prefetch_option = FMAN_PORT_DEQ_FULL_PREFETCH;
  1012. cfg->tx_fifo_deq_pipeline_depth =
  1013. BMI_DEQUEUE_PIPELINE_DEPTH(port->port_type, port->port_speed);
  1014. cfg->deq_byte_cnt = QMI_BYTE_COUNT_LEVEL_CONTROL(port->port_type);
  1015. cfg->rx_pri_elevation =
  1016. DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(port->max_port_fifo_size);
  1017. port->cfg->rx_fifo_thr =
  1018. DFLT_PORT_RX_FIFO_THRESHOLD(port->rev_info.major,
  1019. port->max_port_fifo_size);
  1020. if ((port->rev_info.major == 6) &&
  1021. ((port->rev_info.minor == 0) || (port->rev_info.minor == 3)))
  1022. cfg->errata_A006320 = true;
  1023. /* Excessive Threshold register - exists for pre-FMv3 chips only */
  1024. if (port->rev_info.major < 6)
  1025. cfg->excessive_threshold_register = true;
  1026. else
  1027. cfg->fmbm_tfne_has_features = true;
  1028. cfg->buffer_prefix_content.data_align =
  1029. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1030. }
  1031. static void set_rx_dflt_cfg(struct fman_port *port,
  1032. struct fman_port_params *port_params)
  1033. {
  1034. port->cfg->discard_mask = DFLT_PORT_ERRORS_TO_DISCARD;
  1035. memcpy(&port->cfg->ext_buf_pools,
  1036. &port_params->specific_params.rx_params.ext_buf_pools,
  1037. sizeof(struct fman_ext_pools));
  1038. port->cfg->err_fqid =
  1039. port_params->specific_params.rx_params.err_fqid;
  1040. port->cfg->dflt_fqid =
  1041. port_params->specific_params.rx_params.dflt_fqid;
  1042. }
  1043. static void set_tx_dflt_cfg(struct fman_port *port,
  1044. struct fman_port_params *port_params,
  1045. struct fman_port_dts_params *dts_params)
  1046. {
  1047. port->cfg->tx_fifo_deq_pipeline_depth =
  1048. get_dflt_fifo_deq_pipeline_depth(port->rev_info.major,
  1049. port->port_type,
  1050. port->port_speed);
  1051. port->cfg->err_fqid =
  1052. port_params->specific_params.non_rx_params.err_fqid;
  1053. port->cfg->deq_sp =
  1054. (u8)(dts_params->qman_channel_id & QMI_DEQ_CFG_SUBPORTAL_MASK);
  1055. port->cfg->dflt_fqid =
  1056. port_params->specific_params.non_rx_params.dflt_fqid;
  1057. port->cfg->deq_high_priority = true;
  1058. }
  1059. /**
  1060. * fman_port_config
  1061. * @port: Pointer to the port structure
  1062. * @params: Pointer to data structure of parameters
  1063. *
  1064. * Creates a descriptor for the FM PORT module.
  1065. * The routine returns a pointer to the FM PORT object.
  1066. * This descriptor must be passed as first parameter to all other FM PORT
  1067. * function calls.
  1068. * No actual initialization or configuration of FM hardware is done by this
  1069. * routine.
  1070. *
  1071. * Return: 0 on success; Error code otherwise.
  1072. */
  1073. int fman_port_config(struct fman_port *port, struct fman_port_params *params)
  1074. {
  1075. void __iomem *base_addr = port->dts_params.base_addr;
  1076. int err;
  1077. /* Allocate the FM driver's parameters structure */
  1078. port->cfg = kzalloc(sizeof(*port->cfg), GFP_KERNEL);
  1079. if (!port->cfg)
  1080. goto err_params;
  1081. /* Initialize FM port parameters which will be kept by the driver */
  1082. port->port_type = port->dts_params.type;
  1083. port->port_speed = port->dts_params.speed;
  1084. port->port_id = port->dts_params.id;
  1085. port->fm = port->dts_params.fman;
  1086. port->ext_pools_num = (u8)8;
  1087. /* get FM revision */
  1088. fman_get_revision(port->fm, &port->rev_info);
  1089. err = fill_soc_specific_params(port);
  1090. if (err)
  1091. goto err_port_cfg;
  1092. switch (port->port_type) {
  1093. case FMAN_PORT_TYPE_RX:
  1094. set_rx_dflt_cfg(port, params);
  1095. case FMAN_PORT_TYPE_TX:
  1096. set_tx_dflt_cfg(port, params, &port->dts_params);
  1097. default:
  1098. set_dflt_cfg(port, params);
  1099. }
  1100. /* Continue with other parameters */
  1101. /* set memory map pointers */
  1102. port->bmi_regs = base_addr + BMI_PORT_REGS_OFFSET;
  1103. port->qmi_regs = base_addr + QMI_PORT_REGS_OFFSET;
  1104. port->max_frame_length = DFLT_PORT_MAX_FRAME_LENGTH;
  1105. /* resource distribution. */
  1106. port->fifo_bufs.num =
  1107. get_dflt_num_of_fifo_bufs(port->rev_info.major, port->port_type,
  1108. port->port_speed) * FMAN_BMI_FIFO_UNITS;
  1109. port->fifo_bufs.extra =
  1110. DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS * FMAN_BMI_FIFO_UNITS;
  1111. port->open_dmas.num =
  1112. get_dflt_num_of_open_dmas(port->rev_info.major,
  1113. port->port_type, port->port_speed);
  1114. port->open_dmas.extra =
  1115. get_dflt_extra_num_of_open_dmas(port->rev_info.major,
  1116. port->port_type, port->port_speed);
  1117. port->tasks.num =
  1118. get_dflt_num_of_tasks(port->rev_info.major,
  1119. port->port_type, port->port_speed);
  1120. port->tasks.extra =
  1121. get_dflt_extra_num_of_tasks(port->rev_info.major,
  1122. port->port_type, port->port_speed);
  1123. /* FM_HEAVY_TRAFFIC_SEQUENCER_HANG_ERRATA_FMAN_A006981 errata
  1124. * workaround
  1125. */
  1126. if ((port->rev_info.major == 6) && (port->rev_info.minor == 0) &&
  1127. (((port->port_type == FMAN_PORT_TYPE_TX) &&
  1128. (port->port_speed == 1000)))) {
  1129. port->open_dmas.num = 16;
  1130. port->open_dmas.extra = 0;
  1131. }
  1132. if (port->rev_info.major >= 6 &&
  1133. port->port_type == FMAN_PORT_TYPE_TX &&
  1134. port->port_speed == 1000) {
  1135. /* FM_WRONG_RESET_VALUES_ERRATA_FMAN_A005127 Errata
  1136. * workaround
  1137. */
  1138. if (port->rev_info.major >= 6) {
  1139. u32 reg;
  1140. reg = 0x00001013;
  1141. iowrite32be(reg, &port->bmi_regs->tx.fmbm_tfp);
  1142. }
  1143. }
  1144. return 0;
  1145. err_port_cfg:
  1146. kfree(port->cfg);
  1147. err_params:
  1148. kfree(port);
  1149. return -EINVAL;
  1150. }
  1151. EXPORT_SYMBOL(fman_port_config);
  1152. /**
  1153. * fman_port_init
  1154. * port: A pointer to a FM Port module.
  1155. * Initializes the FM PORT module by defining the software structure and
  1156. * configuring the hardware registers.
  1157. *
  1158. * Return: 0 on success; Error code otherwise.
  1159. */
  1160. int fman_port_init(struct fman_port *port)
  1161. {
  1162. struct fman_port_cfg *cfg;
  1163. int err;
  1164. struct fman_port_init_params params;
  1165. if (is_init_done(port->cfg))
  1166. return -EINVAL;
  1167. err = fman_sp_build_buffer_struct(&port->cfg->int_context,
  1168. &port->cfg->buffer_prefix_content,
  1169. &port->cfg->buf_margins,
  1170. &port->buffer_offsets,
  1171. &port->internal_buf_offset);
  1172. if (err)
  1173. return err;
  1174. cfg = port->cfg;
  1175. if (port->port_type == FMAN_PORT_TYPE_RX) {
  1176. /* Call the external Buffer routine which also checks fifo
  1177. * size and updates it if necessary
  1178. */
  1179. /* define external buffer pools and pool depletion */
  1180. err = set_ext_buffer_pools(port);
  1181. if (err)
  1182. return err;
  1183. /* check if the largest external buffer pool is large enough */
  1184. if (cfg->buf_margins.start_margins + MIN_EXT_BUF_SIZE +
  1185. cfg->buf_margins.end_margins >
  1186. port->rx_pools_params.largest_buf_size) {
  1187. dev_err(port->dev, "%s: buf_margins.start_margins (%d) + minimum buf size (64) + buf_margins.end_margins (%d) is larger than maximum external buffer size (%d)\n",
  1188. __func__, cfg->buf_margins.start_margins,
  1189. cfg->buf_margins.end_margins,
  1190. port->rx_pools_params.largest_buf_size);
  1191. return -EINVAL;
  1192. }
  1193. }
  1194. /* Call FM module routine for communicating parameters */
  1195. memset(&params, 0, sizeof(params));
  1196. params.port_id = port->port_id;
  1197. params.port_type = port->port_type;
  1198. params.port_speed = port->port_speed;
  1199. params.num_of_tasks = (u8)port->tasks.num;
  1200. params.num_of_extra_tasks = (u8)port->tasks.extra;
  1201. params.num_of_open_dmas = (u8)port->open_dmas.num;
  1202. params.num_of_extra_open_dmas = (u8)port->open_dmas.extra;
  1203. if (port->fifo_bufs.num) {
  1204. err = verify_size_of_fifo(port);
  1205. if (err)
  1206. return err;
  1207. }
  1208. params.size_of_fifo = port->fifo_bufs.num;
  1209. params.extra_size_of_fifo = port->fifo_bufs.extra;
  1210. params.deq_pipeline_depth = port->cfg->tx_fifo_deq_pipeline_depth;
  1211. params.max_frame_length = port->max_frame_length;
  1212. err = fman_set_port_params(port->fm, &params);
  1213. if (err)
  1214. return err;
  1215. err = init_low_level_driver(port);
  1216. if (err)
  1217. return err;
  1218. kfree(port->cfg);
  1219. port->cfg = NULL;
  1220. return 0;
  1221. }
  1222. EXPORT_SYMBOL(fman_port_init);
  1223. /**
  1224. * fman_port_cfg_buf_prefix_content
  1225. * @port A pointer to a FM Port module.
  1226. * @buffer_prefix_content A structure of parameters describing
  1227. * the structure of the buffer.
  1228. * Out parameter:
  1229. * Start margin - offset of data from
  1230. * start of external buffer.
  1231. * Defines the structure, size and content of the application buffer.
  1232. * The prefix, in Tx ports, if 'pass_prs_result', the application should set
  1233. * a value to their offsets in the prefix of the FM will save the first
  1234. * 'priv_data_size', than, depending on 'pass_prs_result' and
  1235. * 'pass_time_stamp', copy parse result and timeStamp, and the packet itself
  1236. * (in this order), to the application buffer, and to offset.
  1237. * Calling this routine changes the buffer margins definitions in the internal
  1238. * driver data base from its default configuration:
  1239. * Data size: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PRIV_DATA_SIZE]
  1240. * Pass Parser result: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_PRS_RESULT].
  1241. * Pass timestamp: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_TIME_STAMP].
  1242. * May be used for all ports
  1243. *
  1244. * Allowed only following fman_port_config() and before fman_port_init().
  1245. *
  1246. * Return: 0 on success; Error code otherwise.
  1247. */
  1248. int fman_port_cfg_buf_prefix_content(struct fman_port *port,
  1249. struct fman_buffer_prefix_content *
  1250. buffer_prefix_content)
  1251. {
  1252. if (is_init_done(port->cfg))
  1253. return -EINVAL;
  1254. memcpy(&port->cfg->buffer_prefix_content,
  1255. buffer_prefix_content,
  1256. sizeof(struct fman_buffer_prefix_content));
  1257. /* if data_align was not initialized by user,
  1258. * we return to driver's default
  1259. */
  1260. if (!port->cfg->buffer_prefix_content.data_align)
  1261. port->cfg->buffer_prefix_content.data_align =
  1262. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1263. return 0;
  1264. }
  1265. EXPORT_SYMBOL(fman_port_cfg_buf_prefix_content);
  1266. /**
  1267. * fman_port_disable
  1268. * port: A pointer to a FM Port module.
  1269. *
  1270. * Gracefully disable an FM port. The port will not start new tasks after all
  1271. * tasks associated with the port are terminated.
  1272. *
  1273. * This is a blocking routine, it returns after port is gracefully stopped,
  1274. * i.e. the port will not except new frames, but it will finish all frames
  1275. * or tasks which were already began.
  1276. * Allowed only following fman_port_init().
  1277. *
  1278. * Return: 0 on success; Error code otherwise.
  1279. */
  1280. int fman_port_disable(struct fman_port *port)
  1281. {
  1282. u32 __iomem *bmi_cfg_reg, *bmi_status_reg;
  1283. u32 tmp;
  1284. bool rx_port, failure = false;
  1285. int count;
  1286. if (!is_init_done(port->cfg))
  1287. return -EINVAL;
  1288. switch (port->port_type) {
  1289. case FMAN_PORT_TYPE_RX:
  1290. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1291. bmi_status_reg = &port->bmi_regs->rx.fmbm_rst;
  1292. rx_port = true;
  1293. break;
  1294. case FMAN_PORT_TYPE_TX:
  1295. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1296. bmi_status_reg = &port->bmi_regs->tx.fmbm_tst;
  1297. rx_port = false;
  1298. break;
  1299. default:
  1300. return -EINVAL;
  1301. }
  1302. /* Disable QMI */
  1303. if (!rx_port) {
  1304. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) & ~QMI_PORT_CFG_EN;
  1305. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1306. /* Wait for QMI to finish FD handling */
  1307. count = 100;
  1308. do {
  1309. udelay(10);
  1310. tmp = ioread32be(&port->qmi_regs->fmqm_pns);
  1311. } while ((tmp & QMI_PORT_STATUS_DEQ_FD_BSY) && --count);
  1312. if (count == 0) {
  1313. /* Timeout */
  1314. failure = true;
  1315. }
  1316. }
  1317. /* Disable BMI */
  1318. tmp = ioread32be(bmi_cfg_reg) & ~BMI_PORT_CFG_EN;
  1319. iowrite32be(tmp, bmi_cfg_reg);
  1320. /* Wait for graceful stop end */
  1321. count = 500;
  1322. do {
  1323. udelay(10);
  1324. tmp = ioread32be(bmi_status_reg);
  1325. } while ((tmp & BMI_PORT_STATUS_BSY) && --count);
  1326. if (count == 0) {
  1327. /* Timeout */
  1328. failure = true;
  1329. }
  1330. if (failure)
  1331. dev_dbg(port->dev, "%s: FMan Port[%d]: BMI or QMI is Busy. Port forced down\n",
  1332. __func__, port->port_id);
  1333. return 0;
  1334. }
  1335. EXPORT_SYMBOL(fman_port_disable);
  1336. /**
  1337. * fman_port_enable
  1338. * port: A pointer to a FM Port module.
  1339. *
  1340. * A runtime routine provided to allow disable/enable of port.
  1341. *
  1342. * Allowed only following fman_port_init().
  1343. *
  1344. * Return: 0 on success; Error code otherwise.
  1345. */
  1346. int fman_port_enable(struct fman_port *port)
  1347. {
  1348. u32 __iomem *bmi_cfg_reg;
  1349. u32 tmp;
  1350. bool rx_port;
  1351. if (!is_init_done(port->cfg))
  1352. return -EINVAL;
  1353. switch (port->port_type) {
  1354. case FMAN_PORT_TYPE_RX:
  1355. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1356. rx_port = true;
  1357. break;
  1358. case FMAN_PORT_TYPE_TX:
  1359. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1360. rx_port = false;
  1361. break;
  1362. default:
  1363. return -EINVAL;
  1364. }
  1365. /* Enable QMI */
  1366. if (!rx_port) {
  1367. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) | QMI_PORT_CFG_EN;
  1368. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1369. }
  1370. /* Enable BMI */
  1371. tmp = ioread32be(bmi_cfg_reg) | BMI_PORT_CFG_EN;
  1372. iowrite32be(tmp, bmi_cfg_reg);
  1373. return 0;
  1374. }
  1375. EXPORT_SYMBOL(fman_port_enable);
  1376. /**
  1377. * fman_port_bind
  1378. * dev: FMan Port OF device pointer
  1379. *
  1380. * Bind to a specific FMan Port.
  1381. *
  1382. * Allowed only after the port was created.
  1383. *
  1384. * Return: A pointer to the FMan port device.
  1385. */
  1386. struct fman_port *fman_port_bind(struct device *dev)
  1387. {
  1388. return (struct fman_port *)(dev_get_drvdata(get_device(dev)));
  1389. }
  1390. EXPORT_SYMBOL(fman_port_bind);
  1391. /**
  1392. * fman_port_get_qman_channel_id
  1393. * port: Pointer to the FMan port devuce
  1394. *
  1395. * Get the QMan channel ID for the specific port
  1396. *
  1397. * Return: QMan channel ID
  1398. */
  1399. u32 fman_port_get_qman_channel_id(struct fman_port *port)
  1400. {
  1401. return port->dts_params.qman_channel_id;
  1402. }
  1403. EXPORT_SYMBOL(fman_port_get_qman_channel_id);
  1404. static int fman_port_probe(struct platform_device *of_dev)
  1405. {
  1406. struct fman_port *port;
  1407. struct fman *fman;
  1408. struct device_node *fm_node, *port_node;
  1409. struct resource res;
  1410. struct resource *dev_res;
  1411. u32 val;
  1412. int err = 0, lenp;
  1413. enum fman_port_type port_type;
  1414. u16 port_speed;
  1415. u8 port_id;
  1416. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1417. if (!port)
  1418. return -ENOMEM;
  1419. port->dev = &of_dev->dev;
  1420. port_node = of_node_get(of_dev->dev.of_node);
  1421. /* Get the FM node */
  1422. fm_node = of_get_parent(port_node);
  1423. if (!fm_node) {
  1424. dev_err(port->dev, "%s: of_get_parent() failed\n", __func__);
  1425. err = -ENODEV;
  1426. goto return_err;
  1427. }
  1428. fman = dev_get_drvdata(&of_find_device_by_node(fm_node)->dev);
  1429. of_node_put(fm_node);
  1430. if (!fman) {
  1431. err = -EINVAL;
  1432. goto return_err;
  1433. }
  1434. err = of_property_read_u32(port_node, "cell-index", &val);
  1435. if (err) {
  1436. dev_err(port->dev, "%s: reading cell-index for %s failed\n",
  1437. __func__, port_node->full_name);
  1438. err = -EINVAL;
  1439. goto return_err;
  1440. }
  1441. port_id = (u8)val;
  1442. port->dts_params.id = port_id;
  1443. if (of_device_is_compatible(port_node, "fsl,fman-v3-port-tx")) {
  1444. port_type = FMAN_PORT_TYPE_TX;
  1445. port_speed = 1000;
  1446. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1447. port_speed = 10000;
  1448. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-tx")) {
  1449. if (port_id >= TX_10G_PORT_BASE)
  1450. port_speed = 10000;
  1451. else
  1452. port_speed = 1000;
  1453. port_type = FMAN_PORT_TYPE_TX;
  1454. } else if (of_device_is_compatible(port_node, "fsl,fman-v3-port-rx")) {
  1455. port_type = FMAN_PORT_TYPE_RX;
  1456. port_speed = 1000;
  1457. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1458. port_speed = 10000;
  1459. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-rx")) {
  1460. if (port_id >= RX_10G_PORT_BASE)
  1461. port_speed = 10000;
  1462. else
  1463. port_speed = 1000;
  1464. port_type = FMAN_PORT_TYPE_RX;
  1465. } else {
  1466. dev_err(port->dev, "%s: Illegal port type\n", __func__);
  1467. err = -EINVAL;
  1468. goto return_err;
  1469. }
  1470. port->dts_params.type = port_type;
  1471. port->dts_params.speed = port_speed;
  1472. if (port_type == FMAN_PORT_TYPE_TX) {
  1473. u32 qman_channel_id;
  1474. qman_channel_id = fman_get_qman_channel_id(fman, port_id);
  1475. if (qman_channel_id == 0) {
  1476. dev_err(port->dev, "%s: incorrect qman-channel-id\n",
  1477. __func__);
  1478. err = -EINVAL;
  1479. goto return_err;
  1480. }
  1481. port->dts_params.qman_channel_id = qman_channel_id;
  1482. }
  1483. err = of_address_to_resource(port_node, 0, &res);
  1484. if (err < 0) {
  1485. dev_err(port->dev, "%s: of_address_to_resource() failed\n",
  1486. __func__);
  1487. err = -ENOMEM;
  1488. goto return_err;
  1489. }
  1490. port->dts_params.fman = fman;
  1491. of_node_put(port_node);
  1492. dev_res = __devm_request_region(port->dev, &res, res.start,
  1493. resource_size(&res), "fman-port");
  1494. if (!dev_res) {
  1495. dev_err(port->dev, "%s: __devm_request_region() failed\n",
  1496. __func__);
  1497. err = -EINVAL;
  1498. goto free_port;
  1499. }
  1500. port->dts_params.base_addr = devm_ioremap(port->dev, res.start,
  1501. resource_size(&res));
  1502. if (!port->dts_params.base_addr)
  1503. dev_err(port->dev, "%s: devm_ioremap() failed\n", __func__);
  1504. dev_set_drvdata(&of_dev->dev, port);
  1505. return 0;
  1506. return_err:
  1507. of_node_put(port_node);
  1508. free_port:
  1509. kfree(port);
  1510. return err;
  1511. }
  1512. static const struct of_device_id fman_port_match[] = {
  1513. {.compatible = "fsl,fman-v3-port-rx"},
  1514. {.compatible = "fsl,fman-v2-port-rx"},
  1515. {.compatible = "fsl,fman-v3-port-tx"},
  1516. {.compatible = "fsl,fman-v2-port-tx"},
  1517. {}
  1518. };
  1519. MODULE_DEVICE_TABLE(of, fman_port_match);
  1520. static struct platform_driver fman_port_driver = {
  1521. .driver = {
  1522. .name = "fsl-fman-port",
  1523. .of_match_table = fman_port_match,
  1524. },
  1525. .probe = fman_port_probe,
  1526. };
  1527. static int __init fman_port_load(void)
  1528. {
  1529. int err;
  1530. pr_debug("FSL DPAA FMan driver\n");
  1531. err = platform_driver_register(&fman_port_driver);
  1532. if (err < 0)
  1533. pr_err("Error, platform_driver_register() = %d\n", err);
  1534. return err;
  1535. }
  1536. module_init(fman_port_load);
  1537. static void __exit fman_port_unload(void)
  1538. {
  1539. platform_driver_unregister(&fman_port_driver);
  1540. }
  1541. module_exit(fman_port_unload);
  1542. MODULE_LICENSE("Dual BSD/GPL");
  1543. MODULE_DESCRIPTION("Freescale DPAA Frame Manager Port driver");