fman_memac.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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_memac.h"
  34. #include "fman.h"
  35. #include <linux/slab.h>
  36. #include <linux/io.h>
  37. #include <linux/phy.h>
  38. #include <linux/phy_fixed.h>
  39. #include <linux/of_mdio.h>
  40. /* PCS registers */
  41. #define MDIO_SGMII_CR 0x00
  42. #define MDIO_SGMII_DEV_ABIL_SGMII 0x04
  43. #define MDIO_SGMII_LINK_TMR_L 0x12
  44. #define MDIO_SGMII_LINK_TMR_H 0x13
  45. #define MDIO_SGMII_IF_MODE 0x14
  46. /* SGMII Control defines */
  47. #define SGMII_CR_AN_EN 0x1000
  48. #define SGMII_CR_RESTART_AN 0x0200
  49. #define SGMII_CR_FD 0x0100
  50. #define SGMII_CR_SPEED_SEL1_1G 0x0040
  51. #define SGMII_CR_DEF_VAL (SGMII_CR_AN_EN | SGMII_CR_FD | \
  52. SGMII_CR_SPEED_SEL1_1G)
  53. /* SGMII Device Ability for SGMII defines */
  54. #define MDIO_SGMII_DEV_ABIL_SGMII_MODE 0x4001
  55. #define MDIO_SGMII_DEV_ABIL_BASEX_MODE 0x01A0
  56. /* Link timer define */
  57. #define LINK_TMR_L 0xa120
  58. #define LINK_TMR_H 0x0007
  59. #define LINK_TMR_L_BASEX 0xaf08
  60. #define LINK_TMR_H_BASEX 0x002f
  61. /* SGMII IF Mode defines */
  62. #define IF_MODE_USE_SGMII_AN 0x0002
  63. #define IF_MODE_SGMII_EN 0x0001
  64. #define IF_MODE_SGMII_SPEED_100M 0x0004
  65. #define IF_MODE_SGMII_SPEED_1G 0x0008
  66. #define IF_MODE_SGMII_DUPLEX_HALF 0x0010
  67. /* Num of additional exact match MAC adr regs */
  68. #define MEMAC_NUM_OF_PADDRS 7
  69. /* Control and Configuration Register (COMMAND_CONFIG) */
  70. #define CMD_CFG_REG_LOWP_RXETY 0x01000000 /* 07 Rx low power indication */
  71. #define CMD_CFG_TX_LOWP_ENA 0x00800000 /* 08 Tx Low Power Idle Enable */
  72. #define CMD_CFG_PFC_MODE 0x00080000 /* 12 Enable PFC */
  73. #define CMD_CFG_NO_LEN_CHK 0x00020000 /* 14 Payload length check disable */
  74. #define CMD_CFG_SW_RESET 0x00001000 /* 19 S/W Reset, self clearing bit */
  75. #define CMD_CFG_TX_PAD_EN 0x00000800 /* 20 Enable Tx padding of frames */
  76. #define CMD_CFG_PAUSE_IGNORE 0x00000100 /* 23 Ignore Pause frame quanta */
  77. #define CMD_CFG_CRC_FWD 0x00000040 /* 25 Terminate/frwd CRC of frames */
  78. #define CMD_CFG_PAD_EN 0x00000020 /* 26 Frame padding removal */
  79. #define CMD_CFG_PROMIS_EN 0x00000010 /* 27 Promiscuous operation enable */
  80. #define CMD_CFG_RX_EN 0x00000002 /* 30 MAC receive path enable */
  81. #define CMD_CFG_TX_EN 0x00000001 /* 31 MAC transmit path enable */
  82. /* Transmit FIFO Sections Register (TX_FIFO_SECTIONS) */
  83. #define TX_FIFO_SECTIONS_TX_EMPTY_MASK 0xFFFF0000
  84. #define TX_FIFO_SECTIONS_TX_AVAIL_MASK 0x0000FFFF
  85. #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G 0x00400000
  86. #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G 0x00100000
  87. #define TX_FIFO_SECTIONS_TX_AVAIL_10G 0x00000019
  88. #define TX_FIFO_SECTIONS_TX_AVAIL_1G 0x00000020
  89. #define TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G 0x00000060
  90. #define GET_TX_EMPTY_DEFAULT_VALUE(_val) \
  91. do { \
  92. _val &= ~TX_FIFO_SECTIONS_TX_EMPTY_MASK; \
  93. ((_val == TX_FIFO_SECTIONS_TX_AVAIL_10G) ? \
  94. (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G) :\
  95. (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G));\
  96. } while (0)
  97. /* Interface Mode Register (IF_MODE) */
  98. #define IF_MODE_MASK 0x00000003 /* 30-31 Mask on i/f mode bits */
  99. #define IF_MODE_XGMII 0x00000000 /* 30-31 XGMII (10G) interface */
  100. #define IF_MODE_GMII 0x00000002 /* 30-31 GMII (1G) interface */
  101. #define IF_MODE_RGMII 0x00000004
  102. #define IF_MODE_RGMII_AUTO 0x00008000
  103. #define IF_MODE_RGMII_1000 0x00004000 /* 10 - 1000Mbps RGMII */
  104. #define IF_MODE_RGMII_100 0x00000000 /* 00 - 100Mbps RGMII */
  105. #define IF_MODE_RGMII_10 0x00002000 /* 01 - 10Mbps RGMII */
  106. #define IF_MODE_RGMII_SP_MASK 0x00006000 /* Setsp mask bits */
  107. #define IF_MODE_RGMII_FD 0x00001000 /* Full duplex RGMII */
  108. #define IF_MODE_HD 0x00000040 /* Half duplex operation */
  109. /* Hash table Control Register (HASHTABLE_CTRL) */
  110. #define HASH_CTRL_MCAST_EN 0x00000100
  111. /* 26-31 Hash table address code */
  112. #define HASH_CTRL_ADDR_MASK 0x0000003F
  113. /* MAC mcast indication */
  114. #define GROUP_ADDRESS 0x0000010000000000LL
  115. #define HASH_TABLE_SIZE 64 /* Hash tbl size */
  116. /* Interrupt Mask Register (IMASK) */
  117. #define MEMAC_IMASK_MGI 0x40000000 /* 1 Magic pkt detect indication */
  118. #define MEMAC_IMASK_TSECC_ER 0x20000000 /* 2 Timestamp FIFO ECC error evnt */
  119. #define MEMAC_IMASK_TECC_ER 0x02000000 /* 6 Transmit frame ECC error evnt */
  120. #define MEMAC_IMASK_RECC_ER 0x01000000 /* 7 Receive frame ECC error evnt */
  121. #define MEMAC_ALL_ERRS_IMASK \
  122. ((u32)(MEMAC_IMASK_TSECC_ER | \
  123. MEMAC_IMASK_TECC_ER | \
  124. MEMAC_IMASK_RECC_ER | \
  125. MEMAC_IMASK_MGI))
  126. #define MEMAC_IEVNT_PCS 0x80000000 /* PCS (XG). Link sync (G) */
  127. #define MEMAC_IEVNT_AN 0x40000000 /* Auto-negotiation */
  128. #define MEMAC_IEVNT_LT 0x20000000 /* Link Training/New page */
  129. #define MEMAC_IEVNT_MGI 0x00004000 /* Magic pkt detection */
  130. #define MEMAC_IEVNT_TS_ECC_ER 0x00002000 /* Timestamp FIFO ECC error*/
  131. #define MEMAC_IEVNT_RX_FIFO_OVFL 0x00001000 /* Rx FIFO overflow */
  132. #define MEMAC_IEVNT_TX_FIFO_UNFL 0x00000800 /* Tx FIFO underflow */
  133. #define MEMAC_IEVNT_TX_FIFO_OVFL 0x00000400 /* Tx FIFO overflow */
  134. #define MEMAC_IEVNT_TX_ECC_ER 0x00000200 /* Tx frame ECC error */
  135. #define MEMAC_IEVNT_RX_ECC_ER 0x00000100 /* Rx frame ECC error */
  136. #define MEMAC_IEVNT_LI_FAULT 0x00000080 /* Link Interruption flt */
  137. #define MEMAC_IEVNT_RX_EMPTY 0x00000040 /* Rx FIFO empty */
  138. #define MEMAC_IEVNT_TX_EMPTY 0x00000020 /* Tx FIFO empty */
  139. #define MEMAC_IEVNT_RX_LOWP 0x00000010 /* Low Power Idle */
  140. #define MEMAC_IEVNT_PHY_LOS 0x00000004 /* Phy loss of signal */
  141. #define MEMAC_IEVNT_REM_FAULT 0x00000002 /* Remote fault (XGMII) */
  142. #define MEMAC_IEVNT_LOC_FAULT 0x00000001 /* Local fault (XGMII) */
  143. #define DEFAULT_PAUSE_QUANTA 0xf000
  144. #define DEFAULT_FRAME_LENGTH 0x600
  145. #define DEFAULT_TX_IPG_LENGTH 12
  146. #define CLXY_PAUSE_QUANTA_CLX_PQNT 0x0000FFFF
  147. #define CLXY_PAUSE_QUANTA_CLY_PQNT 0xFFFF0000
  148. #define CLXY_PAUSE_THRESH_CLX_QTH 0x0000FFFF
  149. #define CLXY_PAUSE_THRESH_CLY_QTH 0xFFFF0000
  150. struct mac_addr {
  151. /* Lower 32 bits of 48-bit MAC address */
  152. u32 mac_addr_l;
  153. /* Upper 16 bits of 48-bit MAC address */
  154. u32 mac_addr_u;
  155. };
  156. /* memory map */
  157. struct memac_regs {
  158. u32 res0000[2]; /* General Control and Status */
  159. u32 command_config; /* 0x008 Ctrl and cfg */
  160. struct mac_addr mac_addr0; /* 0x00C-0x010 MAC_ADDR_0...1 */
  161. u32 maxfrm; /* 0x014 Max frame length */
  162. u32 res0018[1];
  163. u32 rx_fifo_sections; /* Receive FIFO configuration reg */
  164. u32 tx_fifo_sections; /* Transmit FIFO configuration reg */
  165. u32 res0024[2];
  166. u32 hashtable_ctrl; /* 0x02C Hash table control */
  167. u32 res0030[4];
  168. u32 ievent; /* 0x040 Interrupt event */
  169. u32 tx_ipg_length; /* 0x044 Transmitter inter-packet-gap */
  170. u32 res0048;
  171. u32 imask; /* 0x04C Interrupt mask */
  172. u32 res0050;
  173. u32 pause_quanta[4]; /* 0x054 Pause quanta */
  174. u32 pause_thresh[4]; /* 0x064 Pause quanta threshold */
  175. u32 rx_pause_status; /* 0x074 Receive pause status */
  176. u32 res0078[2];
  177. struct mac_addr mac_addr[MEMAC_NUM_OF_PADDRS];/* 0x80-0x0B4 mac padr */
  178. u32 lpwake_timer; /* 0x0B8 Low Power Wakeup Timer */
  179. u32 sleep_timer; /* 0x0BC Transmit EEE Low Power Timer */
  180. u32 res00c0[8];
  181. u32 statn_config; /* 0x0E0 Statistics configuration */
  182. u32 res00e4[7];
  183. /* Rx Statistics Counter */
  184. u32 reoct_l;
  185. u32 reoct_u;
  186. u32 roct_l;
  187. u32 roct_u;
  188. u32 raln_l;
  189. u32 raln_u;
  190. u32 rxpf_l;
  191. u32 rxpf_u;
  192. u32 rfrm_l;
  193. u32 rfrm_u;
  194. u32 rfcs_l;
  195. u32 rfcs_u;
  196. u32 rvlan_l;
  197. u32 rvlan_u;
  198. u32 rerr_l;
  199. u32 rerr_u;
  200. u32 ruca_l;
  201. u32 ruca_u;
  202. u32 rmca_l;
  203. u32 rmca_u;
  204. u32 rbca_l;
  205. u32 rbca_u;
  206. u32 rdrp_l;
  207. u32 rdrp_u;
  208. u32 rpkt_l;
  209. u32 rpkt_u;
  210. u32 rund_l;
  211. u32 rund_u;
  212. u32 r64_l;
  213. u32 r64_u;
  214. u32 r127_l;
  215. u32 r127_u;
  216. u32 r255_l;
  217. u32 r255_u;
  218. u32 r511_l;
  219. u32 r511_u;
  220. u32 r1023_l;
  221. u32 r1023_u;
  222. u32 r1518_l;
  223. u32 r1518_u;
  224. u32 r1519x_l;
  225. u32 r1519x_u;
  226. u32 rovr_l;
  227. u32 rovr_u;
  228. u32 rjbr_l;
  229. u32 rjbr_u;
  230. u32 rfrg_l;
  231. u32 rfrg_u;
  232. u32 rcnp_l;
  233. u32 rcnp_u;
  234. u32 rdrntp_l;
  235. u32 rdrntp_u;
  236. u32 res01d0[12];
  237. /* Tx Statistics Counter */
  238. u32 teoct_l;
  239. u32 teoct_u;
  240. u32 toct_l;
  241. u32 toct_u;
  242. u32 res0210[2];
  243. u32 txpf_l;
  244. u32 txpf_u;
  245. u32 tfrm_l;
  246. u32 tfrm_u;
  247. u32 tfcs_l;
  248. u32 tfcs_u;
  249. u32 tvlan_l;
  250. u32 tvlan_u;
  251. u32 terr_l;
  252. u32 terr_u;
  253. u32 tuca_l;
  254. u32 tuca_u;
  255. u32 tmca_l;
  256. u32 tmca_u;
  257. u32 tbca_l;
  258. u32 tbca_u;
  259. u32 res0258[2];
  260. u32 tpkt_l;
  261. u32 tpkt_u;
  262. u32 tund_l;
  263. u32 tund_u;
  264. u32 t64_l;
  265. u32 t64_u;
  266. u32 t127_l;
  267. u32 t127_u;
  268. u32 t255_l;
  269. u32 t255_u;
  270. u32 t511_l;
  271. u32 t511_u;
  272. u32 t1023_l;
  273. u32 t1023_u;
  274. u32 t1518_l;
  275. u32 t1518_u;
  276. u32 t1519x_l;
  277. u32 t1519x_u;
  278. u32 res02a8[6];
  279. u32 tcnp_l;
  280. u32 tcnp_u;
  281. u32 res02c8[14];
  282. /* Line Interface Control */
  283. u32 if_mode; /* 0x300 Interface Mode Control */
  284. u32 if_status; /* 0x304 Interface Status */
  285. u32 res0308[14];
  286. /* HiGig/2 */
  287. u32 hg_config; /* 0x340 Control and cfg */
  288. u32 res0344[3];
  289. u32 hg_pause_quanta; /* 0x350 Pause quanta */
  290. u32 res0354[3];
  291. u32 hg_pause_thresh; /* 0x360 Pause quanta threshold */
  292. u32 res0364[3];
  293. u32 hgrx_pause_status; /* 0x370 Receive pause status */
  294. u32 hg_fifos_status; /* 0x374 fifos status */
  295. u32 rhm; /* 0x378 rx messages counter */
  296. u32 thm; /* 0x37C tx messages counter */
  297. };
  298. struct memac_cfg {
  299. bool reset_on_init;
  300. bool pause_ignore;
  301. bool promiscuous_mode_enable;
  302. struct fixed_phy_status *fixed_link;
  303. u16 max_frame_length;
  304. u16 pause_quanta;
  305. u32 tx_ipg_length;
  306. };
  307. struct fman_mac {
  308. /* Pointer to MAC memory mapped registers */
  309. struct memac_regs __iomem *regs;
  310. /* MAC address of device */
  311. u64 addr;
  312. /* Ethernet physical interface */
  313. phy_interface_t phy_if;
  314. u16 max_speed;
  315. void *dev_id; /* device cookie used by the exception cbs */
  316. fman_mac_exception_cb *exception_cb;
  317. fman_mac_exception_cb *event_cb;
  318. /* Pointer to driver's global address hash table */
  319. struct eth_hash_t *multicast_addr_hash;
  320. /* Pointer to driver's individual address hash table */
  321. struct eth_hash_t *unicast_addr_hash;
  322. u8 mac_id;
  323. u32 exceptions;
  324. struct memac_cfg *memac_drv_param;
  325. void *fm;
  326. struct fman_rev_info fm_rev_info;
  327. bool basex_if;
  328. struct phy_device *pcsphy;
  329. };
  330. static void add_addr_in_paddr(struct memac_regs __iomem *regs, u8 *adr,
  331. u8 paddr_num)
  332. {
  333. u32 tmp0, tmp1;
  334. tmp0 = (u32)(adr[0] | adr[1] << 8 | adr[2] << 16 | adr[3] << 24);
  335. tmp1 = (u32)(adr[4] | adr[5] << 8);
  336. if (paddr_num == 0) {
  337. iowrite32be(tmp0, &regs->mac_addr0.mac_addr_l);
  338. iowrite32be(tmp1, &regs->mac_addr0.mac_addr_u);
  339. } else {
  340. iowrite32be(tmp0, &regs->mac_addr[paddr_num - 1].mac_addr_l);
  341. iowrite32be(tmp1, &regs->mac_addr[paddr_num - 1].mac_addr_u);
  342. }
  343. }
  344. static int reset(struct memac_regs __iomem *regs)
  345. {
  346. u32 tmp;
  347. int count;
  348. tmp = ioread32be(&regs->command_config);
  349. tmp |= CMD_CFG_SW_RESET;
  350. iowrite32be(tmp, &regs->command_config);
  351. count = 100;
  352. do {
  353. udelay(1);
  354. } while ((ioread32be(&regs->command_config) & CMD_CFG_SW_RESET) &&
  355. --count);
  356. if (count == 0)
  357. return -EBUSY;
  358. return 0;
  359. }
  360. static void set_exception(struct memac_regs __iomem *regs, u32 val,
  361. bool enable)
  362. {
  363. u32 tmp;
  364. tmp = ioread32be(&regs->imask);
  365. if (enable)
  366. tmp |= val;
  367. else
  368. tmp &= ~val;
  369. iowrite32be(tmp, &regs->imask);
  370. }
  371. static int init(struct memac_regs __iomem *regs, struct memac_cfg *cfg,
  372. phy_interface_t phy_if, u16 speed, bool slow_10g_if,
  373. u32 exceptions)
  374. {
  375. u32 tmp;
  376. /* Config */
  377. tmp = 0;
  378. if (cfg->promiscuous_mode_enable)
  379. tmp |= CMD_CFG_PROMIS_EN;
  380. if (cfg->pause_ignore)
  381. tmp |= CMD_CFG_PAUSE_IGNORE;
  382. /* Payload length check disable */
  383. tmp |= CMD_CFG_NO_LEN_CHK;
  384. /* Enable padding of frames in transmit direction */
  385. tmp |= CMD_CFG_TX_PAD_EN;
  386. tmp |= CMD_CFG_CRC_FWD;
  387. iowrite32be(tmp, &regs->command_config);
  388. /* Max Frame Length */
  389. iowrite32be((u32)cfg->max_frame_length, &regs->maxfrm);
  390. /* Pause Time */
  391. iowrite32be((u32)cfg->pause_quanta, &regs->pause_quanta[0]);
  392. iowrite32be((u32)0, &regs->pause_thresh[0]);
  393. /* IF_MODE */
  394. tmp = 0;
  395. switch (phy_if) {
  396. case PHY_INTERFACE_MODE_XGMII:
  397. tmp |= IF_MODE_XGMII;
  398. break;
  399. default:
  400. tmp |= IF_MODE_GMII;
  401. if (phy_if == PHY_INTERFACE_MODE_RGMII ||
  402. phy_if == PHY_INTERFACE_MODE_RGMII_ID ||
  403. phy_if == PHY_INTERFACE_MODE_RGMII_RXID ||
  404. phy_if == PHY_INTERFACE_MODE_RGMII_TXID)
  405. tmp |= IF_MODE_RGMII | IF_MODE_RGMII_AUTO;
  406. }
  407. iowrite32be(tmp, &regs->if_mode);
  408. /* TX_FIFO_SECTIONS */
  409. tmp = 0;
  410. if (phy_if == PHY_INTERFACE_MODE_XGMII) {
  411. if (slow_10g_if) {
  412. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G |
  413. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G);
  414. } else {
  415. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_10G |
  416. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G);
  417. }
  418. } else {
  419. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_1G |
  420. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G);
  421. }
  422. iowrite32be(tmp, &regs->tx_fifo_sections);
  423. /* clear all pending events and set-up interrupts */
  424. iowrite32be(0xffffffff, &regs->ievent);
  425. set_exception(regs, exceptions, true);
  426. return 0;
  427. }
  428. static void set_dflts(struct memac_cfg *cfg)
  429. {
  430. cfg->reset_on_init = false;
  431. cfg->promiscuous_mode_enable = false;
  432. cfg->pause_ignore = false;
  433. cfg->tx_ipg_length = DEFAULT_TX_IPG_LENGTH;
  434. cfg->max_frame_length = DEFAULT_FRAME_LENGTH;
  435. cfg->pause_quanta = DEFAULT_PAUSE_QUANTA;
  436. }
  437. static u32 get_mac_addr_hash_code(u64 eth_addr)
  438. {
  439. u64 mask1, mask2;
  440. u32 xor_val = 0;
  441. u8 i, j;
  442. for (i = 0; i < 6; i++) {
  443. mask1 = eth_addr & (u64)0x01;
  444. eth_addr >>= 1;
  445. for (j = 0; j < 7; j++) {
  446. mask2 = eth_addr & (u64)0x01;
  447. mask1 ^= mask2;
  448. eth_addr >>= 1;
  449. }
  450. xor_val |= (mask1 << (5 - i));
  451. }
  452. return xor_val;
  453. }
  454. static void setup_sgmii_internal_phy(struct fman_mac *memac,
  455. struct fixed_phy_status *fixed_link)
  456. {
  457. u16 tmp_reg16;
  458. if (WARN_ON(!memac->pcsphy))
  459. return;
  460. /* SGMII mode */
  461. tmp_reg16 = IF_MODE_SGMII_EN;
  462. if (!fixed_link)
  463. /* AN enable */
  464. tmp_reg16 |= IF_MODE_USE_SGMII_AN;
  465. else {
  466. switch (fixed_link->speed) {
  467. case 10:
  468. /* For 10M: IF_MODE[SPEED_10M] = 0 */
  469. break;
  470. case 100:
  471. tmp_reg16 |= IF_MODE_SGMII_SPEED_100M;
  472. break;
  473. case 1000: /* fallthrough */
  474. default:
  475. tmp_reg16 |= IF_MODE_SGMII_SPEED_1G;
  476. break;
  477. }
  478. if (!fixed_link->duplex)
  479. tmp_reg16 |= IF_MODE_SGMII_DUPLEX_HALF;
  480. }
  481. phy_write(memac->pcsphy, MDIO_SGMII_IF_MODE, tmp_reg16);
  482. /* Device ability according to SGMII specification */
  483. tmp_reg16 = MDIO_SGMII_DEV_ABIL_SGMII_MODE;
  484. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  485. /* Adjust link timer for SGMII -
  486. * According to Cisco SGMII specification the timer should be 1.6 ms.
  487. * The link_timer register is configured in units of the clock.
  488. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  489. * unit = 1 / (125*10^6 Hz) = 8 ns.
  490. * 1.6 ms in units of 8 ns = 1.6ms / 8ns = 2*10^5 = 0x30d40
  491. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  492. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  493. * 1.6 ms in units of 3.2 ns = 1.6ms / 3.2ns = 5*10^5 = 0x7a120.
  494. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  495. * we always set up here a value of 2.5 SGMII.
  496. */
  497. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H);
  498. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L);
  499. if (!fixed_link)
  500. /* Restart AN */
  501. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  502. else
  503. /* AN disabled */
  504. tmp_reg16 = SGMII_CR_DEF_VAL & ~SGMII_CR_AN_EN;
  505. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  506. }
  507. static void setup_sgmii_internal_phy_base_x(struct fman_mac *memac)
  508. {
  509. u16 tmp_reg16;
  510. /* AN Device capability */
  511. tmp_reg16 = MDIO_SGMII_DEV_ABIL_BASEX_MODE;
  512. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  513. /* Adjust link timer for SGMII -
  514. * For Serdes 1000BaseX auto-negotiation the timer should be 10 ms.
  515. * The link_timer register is configured in units of the clock.
  516. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  517. * unit = 1 / (125*10^6 Hz) = 8 ns.
  518. * 10 ms in units of 8 ns = 10ms / 8ns = 1250000 = 0x1312d0
  519. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  520. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  521. * 10 ms in units of 3.2 ns = 10ms / 3.2ns = 3125000 = 0x2faf08.
  522. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  523. * we always set up here a value of 2.5 SGMII.
  524. */
  525. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H_BASEX);
  526. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L_BASEX);
  527. /* Restart AN */
  528. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  529. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  530. }
  531. static int check_init_parameters(struct fman_mac *memac)
  532. {
  533. if (memac->addr == 0) {
  534. pr_err("Ethernet MAC must have a valid MAC address\n");
  535. return -EINVAL;
  536. }
  537. if (!memac->exception_cb) {
  538. pr_err("Uninitialized exception handler\n");
  539. return -EINVAL;
  540. }
  541. if (!memac->event_cb) {
  542. pr_warn("Uninitialize event handler\n");
  543. return -EINVAL;
  544. }
  545. return 0;
  546. }
  547. static int get_exception_flag(enum fman_mac_exceptions exception)
  548. {
  549. u32 bit_mask;
  550. switch (exception) {
  551. case FM_MAC_EX_10G_TX_ECC_ER:
  552. bit_mask = MEMAC_IMASK_TECC_ER;
  553. break;
  554. case FM_MAC_EX_10G_RX_ECC_ER:
  555. bit_mask = MEMAC_IMASK_RECC_ER;
  556. break;
  557. case FM_MAC_EX_TS_FIFO_ECC_ERR:
  558. bit_mask = MEMAC_IMASK_TSECC_ER;
  559. break;
  560. case FM_MAC_EX_MAGIC_PACKET_INDICATION:
  561. bit_mask = MEMAC_IMASK_MGI;
  562. break;
  563. default:
  564. bit_mask = 0;
  565. break;
  566. }
  567. return bit_mask;
  568. }
  569. static void memac_err_exception(void *handle)
  570. {
  571. struct fman_mac *memac = (struct fman_mac *)handle;
  572. struct memac_regs __iomem *regs = memac->regs;
  573. u32 event, imask;
  574. event = ioread32be(&regs->ievent);
  575. imask = ioread32be(&regs->imask);
  576. /* Imask include both error and notification/event bits.
  577. * Leaving only error bits enabled by imask.
  578. * The imask error bits are shifted by 16 bits offset from
  579. * their corresponding location in the ievent - hence the >> 16
  580. */
  581. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  582. iowrite32be(event, &regs->ievent);
  583. if (event & MEMAC_IEVNT_TS_ECC_ER)
  584. memac->exception_cb(memac->dev_id, FM_MAC_EX_TS_FIFO_ECC_ERR);
  585. if (event & MEMAC_IEVNT_TX_ECC_ER)
  586. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_TX_ECC_ER);
  587. if (event & MEMAC_IEVNT_RX_ECC_ER)
  588. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_RX_ECC_ER);
  589. }
  590. static void memac_exception(void *handle)
  591. {
  592. struct fman_mac *memac = (struct fman_mac *)handle;
  593. struct memac_regs __iomem *regs = memac->regs;
  594. u32 event, imask;
  595. event = ioread32be(&regs->ievent);
  596. imask = ioread32be(&regs->imask);
  597. /* Imask include both error and notification/event bits.
  598. * Leaving only error bits enabled by imask.
  599. * The imask error bits are shifted by 16 bits offset from
  600. * their corresponding location in the ievent - hence the >> 16
  601. */
  602. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  603. iowrite32be(event, &regs->ievent);
  604. if (event & MEMAC_IEVNT_MGI)
  605. memac->exception_cb(memac->dev_id,
  606. FM_MAC_EX_MAGIC_PACKET_INDICATION);
  607. }
  608. static void free_init_resources(struct fman_mac *memac)
  609. {
  610. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  611. FMAN_INTR_TYPE_ERR);
  612. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  613. FMAN_INTR_TYPE_NORMAL);
  614. /* release the driver's group hash table */
  615. free_hash_table(memac->multicast_addr_hash);
  616. memac->multicast_addr_hash = NULL;
  617. /* release the driver's individual hash table */
  618. free_hash_table(memac->unicast_addr_hash);
  619. memac->unicast_addr_hash = NULL;
  620. }
  621. static bool is_init_done(struct memac_cfg *memac_drv_params)
  622. {
  623. /* Checks if mEMAC driver parameters were initialized */
  624. if (!memac_drv_params)
  625. return true;
  626. return false;
  627. }
  628. int memac_enable(struct fman_mac *memac, enum comm_mode mode)
  629. {
  630. struct memac_regs __iomem *regs = memac->regs;
  631. u32 tmp;
  632. if (!is_init_done(memac->memac_drv_param))
  633. return -EINVAL;
  634. tmp = ioread32be(&regs->command_config);
  635. if (mode & COMM_MODE_RX)
  636. tmp |= CMD_CFG_RX_EN;
  637. if (mode & COMM_MODE_TX)
  638. tmp |= CMD_CFG_TX_EN;
  639. iowrite32be(tmp, &regs->command_config);
  640. return 0;
  641. }
  642. int memac_disable(struct fman_mac *memac, enum comm_mode mode)
  643. {
  644. struct memac_regs __iomem *regs = memac->regs;
  645. u32 tmp;
  646. if (!is_init_done(memac->memac_drv_param))
  647. return -EINVAL;
  648. tmp = ioread32be(&regs->command_config);
  649. if (mode & COMM_MODE_RX)
  650. tmp &= ~CMD_CFG_RX_EN;
  651. if (mode & COMM_MODE_TX)
  652. tmp &= ~CMD_CFG_TX_EN;
  653. iowrite32be(tmp, &regs->command_config);
  654. return 0;
  655. }
  656. int memac_set_promiscuous(struct fman_mac *memac, bool new_val)
  657. {
  658. struct memac_regs __iomem *regs = memac->regs;
  659. u32 tmp;
  660. if (!is_init_done(memac->memac_drv_param))
  661. return -EINVAL;
  662. tmp = ioread32be(&regs->command_config);
  663. if (new_val)
  664. tmp |= CMD_CFG_PROMIS_EN;
  665. else
  666. tmp &= ~CMD_CFG_PROMIS_EN;
  667. iowrite32be(tmp, &regs->command_config);
  668. return 0;
  669. }
  670. int memac_adjust_link(struct fman_mac *memac, u16 speed)
  671. {
  672. struct memac_regs __iomem *regs = memac->regs;
  673. u32 tmp;
  674. if (!is_init_done(memac->memac_drv_param))
  675. return -EINVAL;
  676. tmp = ioread32be(&regs->if_mode);
  677. /* Set full duplex */
  678. tmp &= ~IF_MODE_HD;
  679. if (memac->phy_if == PHY_INTERFACE_MODE_RGMII) {
  680. /* Configure RGMII in manual mode */
  681. tmp &= ~IF_MODE_RGMII_AUTO;
  682. tmp &= ~IF_MODE_RGMII_SP_MASK;
  683. /* Full duplex */
  684. tmp |= IF_MODE_RGMII_FD;
  685. switch (speed) {
  686. case SPEED_1000:
  687. tmp |= IF_MODE_RGMII_1000;
  688. break;
  689. case SPEED_100:
  690. tmp |= IF_MODE_RGMII_100;
  691. break;
  692. case SPEED_10:
  693. tmp |= IF_MODE_RGMII_10;
  694. break;
  695. default:
  696. break;
  697. }
  698. }
  699. iowrite32be(tmp, &regs->if_mode);
  700. return 0;
  701. }
  702. int memac_cfg_max_frame_len(struct fman_mac *memac, u16 new_val)
  703. {
  704. if (is_init_done(memac->memac_drv_param))
  705. return -EINVAL;
  706. memac->memac_drv_param->max_frame_length = new_val;
  707. return 0;
  708. }
  709. int memac_cfg_reset_on_init(struct fman_mac *memac, bool enable)
  710. {
  711. if (is_init_done(memac->memac_drv_param))
  712. return -EINVAL;
  713. memac->memac_drv_param->reset_on_init = enable;
  714. return 0;
  715. }
  716. int memac_cfg_fixed_link(struct fman_mac *memac,
  717. struct fixed_phy_status *fixed_link)
  718. {
  719. if (is_init_done(memac->memac_drv_param))
  720. return -EINVAL;
  721. memac->memac_drv_param->fixed_link = fixed_link;
  722. return 0;
  723. }
  724. int memac_set_tx_pause_frames(struct fman_mac *memac, u8 priority,
  725. u16 pause_time, u16 thresh_time)
  726. {
  727. struct memac_regs __iomem *regs = memac->regs;
  728. u32 tmp;
  729. if (!is_init_done(memac->memac_drv_param))
  730. return -EINVAL;
  731. tmp = ioread32be(&regs->tx_fifo_sections);
  732. GET_TX_EMPTY_DEFAULT_VALUE(tmp);
  733. iowrite32be(tmp, &regs->tx_fifo_sections);
  734. tmp = ioread32be(&regs->command_config);
  735. tmp &= ~CMD_CFG_PFC_MODE;
  736. priority = 0;
  737. iowrite32be(tmp, &regs->command_config);
  738. tmp = ioread32be(&regs->pause_quanta[priority / 2]);
  739. if (priority % 2)
  740. tmp &= CLXY_PAUSE_QUANTA_CLX_PQNT;
  741. else
  742. tmp &= CLXY_PAUSE_QUANTA_CLY_PQNT;
  743. tmp |= ((u32)pause_time << (16 * (priority % 2)));
  744. iowrite32be(tmp, &regs->pause_quanta[priority / 2]);
  745. tmp = ioread32be(&regs->pause_thresh[priority / 2]);
  746. if (priority % 2)
  747. tmp &= CLXY_PAUSE_THRESH_CLX_QTH;
  748. else
  749. tmp &= CLXY_PAUSE_THRESH_CLY_QTH;
  750. tmp |= ((u32)thresh_time << (16 * (priority % 2)));
  751. iowrite32be(tmp, &regs->pause_thresh[priority / 2]);
  752. return 0;
  753. }
  754. int memac_accept_rx_pause_frames(struct fman_mac *memac, bool en)
  755. {
  756. struct memac_regs __iomem *regs = memac->regs;
  757. u32 tmp;
  758. if (!is_init_done(memac->memac_drv_param))
  759. return -EINVAL;
  760. tmp = ioread32be(&regs->command_config);
  761. if (en)
  762. tmp &= ~CMD_CFG_PAUSE_IGNORE;
  763. else
  764. tmp |= CMD_CFG_PAUSE_IGNORE;
  765. iowrite32be(tmp, &regs->command_config);
  766. return 0;
  767. }
  768. int memac_modify_mac_address(struct fman_mac *memac, enet_addr_t *enet_addr)
  769. {
  770. if (!is_init_done(memac->memac_drv_param))
  771. return -EINVAL;
  772. add_addr_in_paddr(memac->regs, (u8 *)(*enet_addr), 0);
  773. return 0;
  774. }
  775. int memac_add_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  776. {
  777. struct memac_regs __iomem *regs = memac->regs;
  778. struct eth_hash_entry *hash_entry;
  779. u32 hash;
  780. u64 addr;
  781. if (!is_init_done(memac->memac_drv_param))
  782. return -EINVAL;
  783. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  784. if (!(addr & GROUP_ADDRESS)) {
  785. /* Unicast addresses not supported in hash */
  786. pr_err("Unicast Address\n");
  787. return -EINVAL;
  788. }
  789. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  790. /* Create element to be added to the driver hash table */
  791. hash_entry = kmalloc(sizeof(*hash_entry), GFP_KERNEL);
  792. if (!hash_entry)
  793. return -ENOMEM;
  794. hash_entry->addr = addr;
  795. INIT_LIST_HEAD(&hash_entry->node);
  796. list_add_tail(&hash_entry->node,
  797. &memac->multicast_addr_hash->lsts[hash]);
  798. iowrite32be(hash | HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  799. return 0;
  800. }
  801. int memac_del_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  802. {
  803. struct memac_regs __iomem *regs = memac->regs;
  804. struct eth_hash_entry *hash_entry = NULL;
  805. struct list_head *pos;
  806. u32 hash;
  807. u64 addr;
  808. if (!is_init_done(memac->memac_drv_param))
  809. return -EINVAL;
  810. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  811. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  812. list_for_each(pos, &memac->multicast_addr_hash->lsts[hash]) {
  813. hash_entry = ETH_HASH_ENTRY_OBJ(pos);
  814. if (hash_entry->addr == addr) {
  815. list_del_init(&hash_entry->node);
  816. kfree(hash_entry);
  817. break;
  818. }
  819. }
  820. if (list_empty(&memac->multicast_addr_hash->lsts[hash]))
  821. iowrite32be(hash & ~HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  822. return 0;
  823. }
  824. int memac_set_exception(struct fman_mac *memac,
  825. enum fman_mac_exceptions exception, bool enable)
  826. {
  827. u32 bit_mask = 0;
  828. if (!is_init_done(memac->memac_drv_param))
  829. return -EINVAL;
  830. bit_mask = get_exception_flag(exception);
  831. if (bit_mask) {
  832. if (enable)
  833. memac->exceptions |= bit_mask;
  834. else
  835. memac->exceptions &= ~bit_mask;
  836. } else {
  837. pr_err("Undefined exception\n");
  838. return -EINVAL;
  839. }
  840. set_exception(memac->regs, bit_mask, enable);
  841. return 0;
  842. }
  843. int memac_init(struct fman_mac *memac)
  844. {
  845. struct memac_cfg *memac_drv_param;
  846. u8 i;
  847. enet_addr_t eth_addr;
  848. bool slow_10g_if = false;
  849. struct fixed_phy_status *fixed_link;
  850. int err;
  851. u32 reg32 = 0;
  852. if (is_init_done(memac->memac_drv_param))
  853. return -EINVAL;
  854. err = check_init_parameters(memac);
  855. if (err)
  856. return err;
  857. memac_drv_param = memac->memac_drv_param;
  858. if (memac->fm_rev_info.major == 6 && memac->fm_rev_info.minor == 4)
  859. slow_10g_if = true;
  860. /* First, reset the MAC if desired. */
  861. if (memac_drv_param->reset_on_init) {
  862. err = reset(memac->regs);
  863. if (err) {
  864. pr_err("mEMAC reset failed\n");
  865. return err;
  866. }
  867. }
  868. /* MAC Address */
  869. MAKE_ENET_ADDR_FROM_UINT64(memac->addr, eth_addr);
  870. add_addr_in_paddr(memac->regs, (u8 *)eth_addr, 0);
  871. fixed_link = memac_drv_param->fixed_link;
  872. init(memac->regs, memac->memac_drv_param, memac->phy_if,
  873. memac->max_speed, slow_10g_if, memac->exceptions);
  874. /* FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320 errata workaround
  875. * Exists only in FMan 6.0 and 6.3.
  876. */
  877. if ((memac->fm_rev_info.major == 6) &&
  878. ((memac->fm_rev_info.minor == 0) ||
  879. (memac->fm_rev_info.minor == 3))) {
  880. /* MAC strips CRC from received frames - this workaround
  881. * should decrease the likelihood of bug appearance
  882. */
  883. reg32 = ioread32be(&memac->regs->command_config);
  884. reg32 &= ~CMD_CFG_CRC_FWD;
  885. iowrite32be(reg32, &memac->regs->command_config);
  886. }
  887. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII) {
  888. /* Configure internal SGMII PHY */
  889. if (memac->basex_if)
  890. setup_sgmii_internal_phy_base_x(memac);
  891. else
  892. setup_sgmii_internal_phy(memac, fixed_link);
  893. } else if (memac->phy_if == PHY_INTERFACE_MODE_QSGMII) {
  894. /* Configure 4 internal SGMII PHYs */
  895. for (i = 0; i < 4; i++) {
  896. u8 qsmgii_phy_addr, phy_addr;
  897. /* QSGMII PHY address occupies 3 upper bits of 5-bit
  898. * phy_address; the lower 2 bits are used to extend
  899. * register address space and access each one of 4
  900. * ports inside QSGMII.
  901. */
  902. phy_addr = memac->pcsphy->mdio.addr;
  903. qsmgii_phy_addr = (u8)((phy_addr << 2) | i);
  904. memac->pcsphy->mdio.addr = qsmgii_phy_addr;
  905. if (memac->basex_if)
  906. setup_sgmii_internal_phy_base_x(memac);
  907. else
  908. setup_sgmii_internal_phy(memac, fixed_link);
  909. memac->pcsphy->mdio.addr = phy_addr;
  910. }
  911. }
  912. /* Max Frame Length */
  913. err = fman_set_mac_max_frame(memac->fm, memac->mac_id,
  914. memac_drv_param->max_frame_length);
  915. if (err) {
  916. pr_err("settings Mac max frame length is FAILED\n");
  917. return err;
  918. }
  919. memac->multicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  920. if (!memac->multicast_addr_hash) {
  921. free_init_resources(memac);
  922. pr_err("allocation hash table is FAILED\n");
  923. return -ENOMEM;
  924. }
  925. memac->unicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  926. if (!memac->unicast_addr_hash) {
  927. free_init_resources(memac);
  928. pr_err("allocation hash table is FAILED\n");
  929. return -ENOMEM;
  930. }
  931. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  932. FMAN_INTR_TYPE_ERR, memac_err_exception, memac);
  933. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  934. FMAN_INTR_TYPE_NORMAL, memac_exception, memac);
  935. kfree(memac_drv_param);
  936. memac->memac_drv_param = NULL;
  937. return 0;
  938. }
  939. int memac_free(struct fman_mac *memac)
  940. {
  941. free_init_resources(memac);
  942. if (memac->pcsphy)
  943. put_device(&memac->pcsphy->mdio.dev);
  944. kfree(memac->memac_drv_param);
  945. kfree(memac);
  946. return 0;
  947. }
  948. struct fman_mac *memac_config(struct fman_mac_params *params)
  949. {
  950. struct fman_mac *memac;
  951. struct memac_cfg *memac_drv_param;
  952. void __iomem *base_addr;
  953. base_addr = params->base_addr;
  954. /* allocate memory for the m_emac data structure */
  955. memac = kzalloc(sizeof(*memac), GFP_KERNEL);
  956. if (!memac)
  957. return NULL;
  958. /* allocate memory for the m_emac driver parameters data structure */
  959. memac_drv_param = kzalloc(sizeof(*memac_drv_param), GFP_KERNEL);
  960. if (!memac_drv_param) {
  961. memac_free(memac);
  962. return NULL;
  963. }
  964. /* Plant parameter structure pointer */
  965. memac->memac_drv_param = memac_drv_param;
  966. set_dflts(memac_drv_param);
  967. memac->addr = ENET_ADDR_TO_UINT64(params->addr);
  968. memac->regs = base_addr;
  969. memac->max_speed = params->max_speed;
  970. memac->phy_if = params->phy_if;
  971. memac->mac_id = params->mac_id;
  972. memac->exceptions = (MEMAC_IMASK_TSECC_ER | MEMAC_IMASK_TECC_ER |
  973. MEMAC_IMASK_RECC_ER | MEMAC_IMASK_MGI);
  974. memac->exception_cb = params->exception_cb;
  975. memac->event_cb = params->event_cb;
  976. memac->dev_id = params->dev_id;
  977. memac->fm = params->fm;
  978. memac->basex_if = params->basex_if;
  979. /* Save FMan revision */
  980. fman_get_revision(memac->fm, &memac->fm_rev_info);
  981. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII ||
  982. memac->phy_if == PHY_INTERFACE_MODE_QSGMII) {
  983. if (!params->internal_phy_node) {
  984. pr_err("PCS PHY node is not available\n");
  985. memac_free(memac);
  986. return NULL;
  987. }
  988. memac->pcsphy = of_phy_find_device(params->internal_phy_node);
  989. if (!memac->pcsphy) {
  990. pr_err("of_phy_find_device (PCS PHY) failed\n");
  991. memac_free(memac);
  992. return NULL;
  993. }
  994. }
  995. return memac;
  996. }