1 /* 2 * This file is subject to the terms and conditions of the GNU General Public 3 * License. See the file "COPYING" in the main directory of this archive 4 * for more details. 5 * 6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/mutex.h> 11 #include <linux/err.h> 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <bcm63xx_cpu.h> 15 #include <bcm63xx_io.h> 16 #include <bcm63xx_regs.h> 17 #include <bcm63xx_reset.h> 18 19 struct clk { 20 void (*set)(struct clk *, int); 21 unsigned int rate; 22 unsigned int usage; 23 int id; 24 }; 25 26 static DEFINE_MUTEX(clocks_mutex); 27 28 29 static void clk_enable_unlocked(struct clk *clk) 30 { 31 if (clk->set && (clk->usage++) == 0) 32 clk->set(clk, 1); 33 } 34 35 static void clk_disable_unlocked(struct clk *clk) 36 { 37 if (clk->set && (--clk->usage) == 0) 38 clk->set(clk, 0); 39 } 40 41 static void bcm_hwclock_set(u32 mask, int enable) 42 { 43 u32 reg; 44 45 reg = bcm_perf_readl(PERF_CKCTL_REG); 46 if (enable) 47 reg |= mask; 48 else 49 reg &= ~mask; 50 bcm_perf_writel(reg, PERF_CKCTL_REG); 51 } 52 53 /* 54 * Ethernet MAC "misc" clock: dma clocks and main clock on 6348 55 */ 56 static void enet_misc_set(struct clk *clk, int enable) 57 { 58 u32 mask; 59 60 if (BCMCPU_IS_6338()) 61 mask = CKCTL_6338_ENET_EN; 62 else if (BCMCPU_IS_6345()) 63 mask = CKCTL_6345_ENET_EN; 64 else if (BCMCPU_IS_6348()) 65 mask = CKCTL_6348_ENET_EN; 66 else 67 /* BCMCPU_IS_6358 */ 68 mask = CKCTL_6358_EMUSB_EN; 69 bcm_hwclock_set(mask, enable); 70 } 71 72 static struct clk clk_enet_misc = { 73 .set = enet_misc_set, 74 }; 75 76 /* 77 * Ethernet MAC clocks: only revelant on 6358, silently enable misc 78 * clocks 79 */ 80 static void enetx_set(struct clk *clk, int enable) 81 { 82 if (enable) 83 clk_enable_unlocked(&clk_enet_misc); 84 else 85 clk_disable_unlocked(&clk_enet_misc); 86 87 if (BCMCPU_IS_6358()) { 88 u32 mask; 89 90 if (clk->id == 0) 91 mask = CKCTL_6358_ENET0_EN; 92 else 93 mask = CKCTL_6358_ENET1_EN; 94 bcm_hwclock_set(mask, enable); 95 } 96 } 97 98 static struct clk clk_enet0 = { 99 .id = 0, 100 .set = enetx_set, 101 }; 102 103 static struct clk clk_enet1 = { 104 .id = 1, 105 .set = enetx_set, 106 }; 107 108 /* 109 * Ethernet PHY clock 110 */ 111 static void ephy_set(struct clk *clk, int enable) 112 { 113 if (!BCMCPU_IS_6358()) 114 return; 115 bcm_hwclock_set(CKCTL_6358_EPHY_EN, enable); 116 } 117 118 119 static struct clk clk_ephy = { 120 .set = ephy_set, 121 }; 122 123 /* 124 * Ethernet switch clock 125 */ 126 static void enetsw_set(struct clk *clk, int enable) 127 { 128 if (!BCMCPU_IS_6368()) 129 return; 130 bcm_hwclock_set(CKCTL_6368_ROBOSW_EN | 131 CKCTL_6368_SWPKT_USB_EN | 132 CKCTL_6368_SWPKT_SAR_EN, enable); 133 if (enable) { 134 /* reset switch core afer clock change */ 135 bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 1); 136 msleep(10); 137 bcm63xx_core_set_reset(BCM63XX_RESET_ENETSW, 0); 138 msleep(10); 139 } 140 } 141 142 static struct clk clk_enetsw = { 143 .set = enetsw_set, 144 }; 145 146 /* 147 * PCM clock 148 */ 149 static void pcm_set(struct clk *clk, int enable) 150 { 151 if (!BCMCPU_IS_6358()) 152 return; 153 bcm_hwclock_set(CKCTL_6358_PCM_EN, enable); 154 } 155 156 static struct clk clk_pcm = { 157 .set = pcm_set, 158 }; 159 160 /* 161 * USB host clock 162 */ 163 static void usbh_set(struct clk *clk, int enable) 164 { 165 if (BCMCPU_IS_6328()) 166 bcm_hwclock_set(CKCTL_6328_USBH_EN, enable); 167 else if (BCMCPU_IS_6348()) 168 bcm_hwclock_set(CKCTL_6348_USBH_EN, enable); 169 else if (BCMCPU_IS_6368()) 170 bcm_hwclock_set(CKCTL_6368_USBH_EN, enable); 171 } 172 173 static struct clk clk_usbh = { 174 .set = usbh_set, 175 }; 176 177 /* 178 * USB device clock 179 */ 180 static void usbd_set(struct clk *clk, int enable) 181 { 182 if (BCMCPU_IS_6328()) 183 bcm_hwclock_set(CKCTL_6328_USBD_EN, enable); 184 else if (BCMCPU_IS_6368()) 185 bcm_hwclock_set(CKCTL_6368_USBD_EN, enable); 186 } 187 188 static struct clk clk_usbd = { 189 .set = usbd_set, 190 }; 191 192 /* 193 * SPI clock 194 */ 195 static void spi_set(struct clk *clk, int enable) 196 { 197 u32 mask; 198 199 if (BCMCPU_IS_6338()) 200 mask = CKCTL_6338_SPI_EN; 201 else if (BCMCPU_IS_6348()) 202 mask = CKCTL_6348_SPI_EN; 203 else if (BCMCPU_IS_6358()) 204 mask = CKCTL_6358_SPI_EN; 205 else if (BCMCPU_IS_6362()) 206 mask = CKCTL_6362_SPI_EN; 207 else 208 /* BCMCPU_IS_6368 */ 209 mask = CKCTL_6368_SPI_EN; 210 bcm_hwclock_set(mask, enable); 211 } 212 213 static struct clk clk_spi = { 214 .set = spi_set, 215 }; 216 217 /* 218 * XTM clock 219 */ 220 static void xtm_set(struct clk *clk, int enable) 221 { 222 if (!BCMCPU_IS_6368()) 223 return; 224 225 bcm_hwclock_set(CKCTL_6368_SAR_EN | 226 CKCTL_6368_SWPKT_SAR_EN, enable); 227 228 if (enable) { 229 /* reset sar core afer clock change */ 230 bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 1); 231 mdelay(1); 232 bcm63xx_core_set_reset(BCM63XX_RESET_SAR, 0); 233 mdelay(1); 234 } 235 } 236 237 238 static struct clk clk_xtm = { 239 .set = xtm_set, 240 }; 241 242 /* 243 * IPsec clock 244 */ 245 static void ipsec_set(struct clk *clk, int enable) 246 { 247 bcm_hwclock_set(CKCTL_6368_IPSEC_EN, enable); 248 } 249 250 static struct clk clk_ipsec = { 251 .set = ipsec_set, 252 }; 253 254 /* 255 * PCIe clock 256 */ 257 258 static void pcie_set(struct clk *clk, int enable) 259 { 260 bcm_hwclock_set(CKCTL_6328_PCIE_EN, enable); 261 } 262 263 static struct clk clk_pcie = { 264 .set = pcie_set, 265 }; 266 267 /* 268 * Internal peripheral clock 269 */ 270 static struct clk clk_periph = { 271 .rate = (50 * 1000 * 1000), 272 }; 273 274 275 /* 276 * Linux clock API implementation 277 */ 278 int clk_enable(struct clk *clk) 279 { 280 mutex_lock(&clocks_mutex); 281 clk_enable_unlocked(clk); 282 mutex_unlock(&clocks_mutex); 283 return 0; 284 } 285 286 EXPORT_SYMBOL(clk_enable); 287 288 void clk_disable(struct clk *clk) 289 { 290 mutex_lock(&clocks_mutex); 291 clk_disable_unlocked(clk); 292 mutex_unlock(&clocks_mutex); 293 } 294 295 EXPORT_SYMBOL(clk_disable); 296 297 unsigned long clk_get_rate(struct clk *clk) 298 { 299 return clk->rate; 300 } 301 302 EXPORT_SYMBOL(clk_get_rate); 303 304 struct clk *clk_get(struct device *dev, const char *id) 305 { 306 if (!strcmp(id, "enet0")) 307 return &clk_enet0; 308 if (!strcmp(id, "enet1")) 309 return &clk_enet1; 310 if (!strcmp(id, "enetsw")) 311 return &clk_enetsw; 312 if (!strcmp(id, "ephy")) 313 return &clk_ephy; 314 if (!strcmp(id, "usbh")) 315 return &clk_usbh; 316 if (!strcmp(id, "usbd")) 317 return &clk_usbd; 318 if (!strcmp(id, "spi")) 319 return &clk_spi; 320 if (!strcmp(id, "xtm")) 321 return &clk_xtm; 322 if (!strcmp(id, "periph")) 323 return &clk_periph; 324 if (BCMCPU_IS_6358() && !strcmp(id, "pcm")) 325 return &clk_pcm; 326 if (BCMCPU_IS_6368() && !strcmp(id, "ipsec")) 327 return &clk_ipsec; 328 if (BCMCPU_IS_6328() && !strcmp(id, "pcie")) 329 return &clk_pcie; 330 return ERR_PTR(-ENOENT); 331 } 332 333 EXPORT_SYMBOL(clk_get); 334 335 void clk_put(struct clk *clk) 336 { 337 } 338 339 EXPORT_SYMBOL(clk_put); 340