1 /* 2 * 3 * tp3780i.c -- board driver for 3780i on ThinkPads 4 * 5 * 6 * Written By: Mike Sullivan IBM Corporation 7 * 8 * Copyright (C) 1999 IBM Corporation 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * NO WARRANTY 21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR 22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT 23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is 25 * solely responsible for determining the appropriateness of using and 26 * distributing the Program and assumes all risks associated with its 27 * exercise of rights under this Agreement, including but not limited to 28 * the risks and costs of program errors, damage to or loss of data, 29 * programs or equipment, and unavailability or interruption of operations. 30 * 31 * DISCLAIMER OF LIABILITY 32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY 33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND 35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 39 * 40 * You should have received a copy of the GNU General Public License 41 * along with this program; if not, write to the Free Software 42 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 43 * 44 * 45 * 10/23/2000 - Alpha Release 46 * First release to the public 47 */ 48 49 #define pr_fmt(fmt) "tp3780i: " fmt 50 51 #include <linux/interrupt.h> 52 #include <linux/kernel.h> 53 #include <linux/ptrace.h> 54 #include <linux/ioport.h> 55 #include <asm/io.h> 56 #include "smapi.h" 57 #include "mwavedd.h" 58 #include "tp3780i.h" 59 #include "3780i.h" 60 #include "mwavepub.h" 61 62 static unsigned short s_ausThinkpadIrqToField[16] = 63 { 0xFFFF, 0xFFFF, 0xFFFF, 0x0001, 0x0002, 0x0003, 0xFFFF, 0x0004, 64 0xFFFF, 0xFFFF, 0x0005, 0x0006, 0xFFFF, 0xFFFF, 0xFFFF, 0x0007 }; 65 static unsigned short s_ausThinkpadDmaToField[8] = 66 { 0x0001, 0x0002, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0003, 0x0004 }; 67 static unsigned short s_numIrqs = 16, s_numDmas = 8; 68 69 70 static void EnableSRAM(struct thinkpad_bd_data *pBDData) 71 { 72 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 73 unsigned short usDspBaseIO = pSettings->usDspBaseIO; 74 DSP_GPIO_OUTPUT_DATA_15_8 rGpioOutputData; 75 DSP_GPIO_DRIVER_ENABLE_15_8 rGpioDriverEnable; 76 DSP_GPIO_MODE_15_8 rGpioMode; 77 78 MKWORD(rGpioMode) = ReadMsaCfg(DSP_GpioModeControl_15_8); 79 rGpioMode.GpioMode10 = 0; 80 WriteMsaCfg(DSP_GpioModeControl_15_8, MKWORD(rGpioMode)); 81 82 MKWORD(rGpioDriverEnable) = 0; 83 rGpioDriverEnable.Enable10 = true; 84 rGpioDriverEnable.Mask10 = true; 85 WriteMsaCfg(DSP_GpioDriverEnable_15_8, MKWORD(rGpioDriverEnable)); 86 87 MKWORD(rGpioOutputData) = 0; 88 rGpioOutputData.Latch10 = 0; 89 rGpioOutputData.Mask10 = true; 90 WriteMsaCfg(DSP_GpioOutputData_15_8, MKWORD(rGpioOutputData)); 91 } 92 93 94 static irqreturn_t UartInterrupt(int irq, void *dev_id) 95 { 96 return IRQ_HANDLED; 97 } 98 99 static irqreturn_t DspInterrupt(int irq, void *dev_id) 100 { 101 struct mwave_device_data *pDrvData = &mwave_s_mdd; 102 struct dsp_3780i_config_settings *pSettings = &pDrvData->rBDData.rDspSettings; 103 unsigned short usDspBaseIO = pSettings->usDspBaseIO; 104 unsigned short usIPCSource = 0, usIsolationMask, usPCNum; 105 106 if (dsp3780I_GetIPCSource(usDspBaseIO, &usIPCSource) == 0) { 107 usIsolationMask = 1; 108 for (usPCNum = 1; usPCNum <= 16; usPCNum++) { 109 if (usIPCSource & usIsolationMask) { 110 usIPCSource &= ~usIsolationMask; 111 if (pDrvData->IPCs[usPCNum - 1].usIntCount == 0) { 112 pDrvData->IPCs[usPCNum - 1].usIntCount = 1; 113 } 114 if (pDrvData->IPCs[usPCNum - 1].bIsEnabled == true) { 115 wake_up_interruptible(&pDrvData->IPCs[usPCNum - 1].ipc_wait_queue); 116 } 117 } 118 if (usIPCSource == 0) 119 break; 120 /* try next IPC */ 121 usIsolationMask = usIsolationMask << 1; 122 } 123 } 124 return IRQ_HANDLED; 125 } 126 127 128 int tp3780I_InitializeBoardData(struct thinkpad_bd_data *pBDData) 129 { 130 int retval = 0; 131 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 132 133 pBDData->bDSPEnabled = false; 134 pSettings->bInterruptClaimed = false; 135 136 retval = smapi_init(); 137 if (retval) { 138 pr_err("%s: Error: SMAPI is not available on this machine\n", __func__); 139 } else { 140 if (mwave_3780i_irq || mwave_3780i_io || mwave_uart_irq || mwave_uart_io) { 141 retval = smapi_set_DSP_cfg(); 142 } 143 } 144 145 return retval; 146 } 147 148 void tp3780I_Cleanup(struct thinkpad_bd_data *pBDData) 149 { 150 } 151 152 int tp3780I_CalcResources(struct thinkpad_bd_data *pBDData) 153 { 154 struct smapi_dsp_settings rSmapiInfo; 155 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 156 157 if (smapi_query_DSP_cfg(&rSmapiInfo)) { 158 pr_err("%s: Error: Could not query DSP config. Aborting.\n", __func__); 159 return -EIO; 160 } 161 162 /* Sanity check */ 163 if ( 164 ( rSmapiInfo.usDspIRQ == 0 ) 165 || ( rSmapiInfo.usDspBaseIO == 0 ) 166 || ( rSmapiInfo.usUartIRQ == 0 ) 167 || ( rSmapiInfo.usUartBaseIO == 0 ) 168 ) { 169 pr_err("%s: Error: Illegal resource setting. Aborting.\n", __func__); 170 return -EIO; 171 } 172 173 pSettings->bDSPEnabled = (rSmapiInfo.bDSPEnabled && rSmapiInfo.bDSPPresent); 174 pSettings->bModemEnabled = rSmapiInfo.bModemEnabled; 175 pSettings->usDspIrq = rSmapiInfo.usDspIRQ; 176 pSettings->usDspDma = rSmapiInfo.usDspDMA; 177 pSettings->usDspBaseIO = rSmapiInfo.usDspBaseIO; 178 pSettings->usUartIrq = rSmapiInfo.usUartIRQ; 179 pSettings->usUartBaseIO = rSmapiInfo.usUartBaseIO; 180 181 pSettings->uDStoreSize = TP_ABILITIES_DATA_SIZE; 182 pSettings->uIStoreSize = TP_ABILITIES_INST_SIZE; 183 pSettings->uIps = TP_ABILITIES_INTS_PER_SEC; 184 185 if (pSettings->bDSPEnabled && pSettings->bModemEnabled && pSettings->usDspIrq == pSettings->usUartIrq) { 186 pBDData->bShareDspIrq = pBDData->bShareUartIrq = 1; 187 } else { 188 pBDData->bShareDspIrq = pBDData->bShareUartIrq = 0; 189 } 190 191 return 0; 192 } 193 194 195 int tp3780I_ClaimResources(struct thinkpad_bd_data *pBDData) 196 { 197 int retval = 0; 198 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 199 struct resource *pres; 200 201 pres = request_region(pSettings->usDspBaseIO, 16, "mwave_3780i"); 202 if ( pres == NULL ) retval = -EIO; 203 204 if (retval) { 205 pr_err("%s: Error: Could not claim I/O region starting at %x\n", __func__, 206 pSettings->usDspBaseIO); 207 return -EIO; 208 } 209 210 return retval; 211 } 212 213 int tp3780I_ReleaseResources(struct thinkpad_bd_data *pBDData) 214 { 215 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 216 217 release_region(pSettings->usDspBaseIO & (~3), 16); 218 219 if (pSettings->bInterruptClaimed) { 220 free_irq(pSettings->usDspIrq, NULL); 221 pSettings->bInterruptClaimed = false; 222 } 223 224 return 0; 225 } 226 227 228 229 int tp3780I_EnableDSP(struct thinkpad_bd_data *pBDData) 230 { 231 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 232 bool bDSPPoweredUp = false, bInterruptAllocated = false; 233 234 if (pBDData->bDSPEnabled) { 235 pr_err("%s: Error: DSP already enabled!\n", __func__); 236 goto exit_cleanup; 237 } 238 239 if (!pSettings->bDSPEnabled) { 240 pr_err("%s: Error: pSettings->bDSPEnabled not set\n", __func__); 241 goto exit_cleanup; 242 } 243 244 if ( 245 (pSettings->usDspIrq >= s_numIrqs) 246 || (pSettings->usDspDma >= s_numDmas) 247 || (s_ausThinkpadIrqToField[pSettings->usDspIrq] == 0xFFFF) 248 || (s_ausThinkpadDmaToField[pSettings->usDspDma] == 0xFFFF) 249 ) { 250 pr_err("%s: Error: invalid irq %x\n", __func__, pSettings->usDspIrq); 251 goto exit_cleanup; 252 } 253 254 if ( 255 ((pSettings->usDspBaseIO & 0xF00F) != 0) 256 || (pSettings->usDspBaseIO & 0x0FF0) == 0 257 ) { 258 pr_err("%s: Error: Invalid DSP base I/O address %x\n", __func__, 259 pSettings->usDspBaseIO); 260 goto exit_cleanup; 261 } 262 263 if (pSettings->bModemEnabled) { 264 if ( 265 pSettings->usUartIrq >= s_numIrqs 266 || s_ausThinkpadIrqToField[pSettings->usUartIrq] == 0xFFFF 267 ) { 268 pr_err("%s: Error: Invalid UART IRQ %x\n", __func__, pSettings->usUartIrq); 269 goto exit_cleanup; 270 } 271 switch (pSettings->usUartBaseIO) { 272 case 0x03F8: 273 case 0x02F8: 274 case 0x03E8: 275 case 0x02E8: 276 break; 277 278 default: 279 pr_err("%s: Error: Invalid UART base I/O address %x\n", __func__, 280 pSettings->usUartBaseIO); 281 goto exit_cleanup; 282 } 283 } 284 285 pSettings->bDspIrqActiveLow = pSettings->bDspIrqPulse = true; 286 pSettings->bUartIrqActiveLow = pSettings->bUartIrqPulse = true; 287 288 if (pBDData->bShareDspIrq) { 289 pSettings->bDspIrqActiveLow = false; 290 } 291 if (pBDData->bShareUartIrq) { 292 pSettings->bUartIrqActiveLow = false; 293 } 294 295 pSettings->usNumTransfers = TP_CFG_NumTransfers; 296 pSettings->usReRequest = TP_CFG_RerequestTimer; 297 pSettings->bEnableMEMCS16 = TP_CFG_MEMCS16; 298 pSettings->usIsaMemCmdWidth = TP_CFG_IsaMemCmdWidth; 299 pSettings->bGateIOCHRDY = TP_CFG_GateIOCHRDY; 300 pSettings->bEnablePwrMgmt = TP_CFG_EnablePwrMgmt; 301 pSettings->usHBusTimerLoadValue = TP_CFG_HBusTimerValue; 302 pSettings->bDisableLBusTimeout = TP_CFG_DisableLBusTimeout; 303 pSettings->usN_Divisor = TP_CFG_N_Divisor; 304 pSettings->usM_Multiplier = TP_CFG_M_Multiplier; 305 pSettings->bPllBypass = TP_CFG_PllBypass; 306 pSettings->usChipletEnable = TP_CFG_ChipletEnable; 307 308 if (request_irq(pSettings->usUartIrq, &UartInterrupt, 0, "mwave_uart", NULL)) { 309 pr_err("%s: Error: Could not get UART IRQ %x\n", __func__, pSettings->usUartIrq); 310 goto exit_cleanup; 311 } else { /* no conflict just release */ 312 free_irq(pSettings->usUartIrq, NULL); 313 } 314 315 if (request_irq(pSettings->usDspIrq, &DspInterrupt, 0, "mwave_3780i", NULL)) { 316 pr_err("%s: Error: Could not get 3780i IRQ %x\n", __func__, pSettings->usDspIrq); 317 goto exit_cleanup; 318 } else { 319 bInterruptAllocated = true; 320 pSettings->bInterruptClaimed = true; 321 } 322 323 smapi_set_DSP_power_state(false); 324 if (smapi_set_DSP_power_state(true)) { 325 pr_err("%s: Error: smapi_set_DSP_power_state(true) failed\n", __func__); 326 goto exit_cleanup; 327 } else { 328 bDSPPoweredUp = true; 329 } 330 331 if (dsp3780I_EnableDSP(pSettings, s_ausThinkpadIrqToField, s_ausThinkpadDmaToField)) { 332 pr_err("%s: Error: dsp7880I_EnableDSP() failed\n", __func__); 333 goto exit_cleanup; 334 } 335 336 EnableSRAM(pBDData); 337 338 pBDData->bDSPEnabled = true; 339 340 return 0; 341 342 exit_cleanup: 343 pr_err("%s: Cleaning up\n", __func__); 344 if (bDSPPoweredUp) 345 smapi_set_DSP_power_state(false); 346 if (bInterruptAllocated) { 347 free_irq(pSettings->usDspIrq, NULL); 348 pSettings->bInterruptClaimed = false; 349 } 350 return -EIO; 351 } 352 353 354 int tp3780I_DisableDSP(struct thinkpad_bd_data *pBDData) 355 { 356 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 357 358 if (pBDData->bDSPEnabled) { 359 dsp3780I_DisableDSP(&pBDData->rDspSettings); 360 if (pSettings->bInterruptClaimed) { 361 free_irq(pSettings->usDspIrq, NULL); 362 pSettings->bInterruptClaimed = false; 363 } 364 smapi_set_DSP_power_state(false); 365 pBDData->bDSPEnabled = false; 366 } 367 368 return 0; 369 } 370 371 372 int tp3780I_ResetDSP(struct thinkpad_bd_data *pBDData) 373 { 374 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 375 376 if (dsp3780I_Reset(pSettings) == 0) { 377 EnableSRAM(pBDData); 378 return 0; 379 } 380 return -EIO; 381 } 382 383 384 int tp3780I_StartDSP(struct thinkpad_bd_data *pBDData) 385 { 386 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 387 388 if (dsp3780I_Run(pSettings) == 0) { 389 // @BUG @TBD EnableSRAM(pBDData); 390 } else { 391 return -EIO; 392 } 393 394 return 0; 395 } 396 397 398 int tp3780I_QueryAbilities(struct thinkpad_bd_data *pBDData, struct mw_abilities *pAbilities) 399 { 400 memset(pAbilities, 0, sizeof(*pAbilities)); 401 /* fill out standard constant fields */ 402 pAbilities->instr_per_sec = pBDData->rDspSettings.uIps; 403 pAbilities->data_size = pBDData->rDspSettings.uDStoreSize; 404 pAbilities->inst_size = pBDData->rDspSettings.uIStoreSize; 405 pAbilities->bus_dma_bw = pBDData->rDspSettings.uDmaBandwidth; 406 407 /* fill out dynamically determined fields */ 408 pAbilities->component_list[0] = 0x00010000 | MW_ADC_MASK; 409 pAbilities->component_list[1] = 0x00010000 | MW_ACI_MASK; 410 pAbilities->component_list[2] = 0x00010000 | MW_AIC1_MASK; 411 pAbilities->component_list[3] = 0x00010000 | MW_AIC2_MASK; 412 pAbilities->component_list[4] = 0x00010000 | MW_CDDAC_MASK; 413 pAbilities->component_list[5] = 0x00010000 | MW_MIDI_MASK; 414 pAbilities->component_list[6] = 0x00010000 | MW_UART_MASK; 415 pAbilities->component_count = 7; 416 417 /* Fill out Mwave OS and BIOS task names */ 418 419 memcpy(pAbilities->mwave_os_name, TP_ABILITIES_MWAVEOS_NAME, 420 sizeof(TP_ABILITIES_MWAVEOS_NAME)); 421 memcpy(pAbilities->bios_task_name, TP_ABILITIES_BIOSTASK_NAME, 422 sizeof(TP_ABILITIES_BIOSTASK_NAME)); 423 424 return 0; 425 } 426 427 int tp3780I_ReadWriteDspDStore(struct thinkpad_bd_data *pBDData, unsigned int uOpcode, 428 void __user *pvBuffer, unsigned int uCount, 429 unsigned long ulDSPAddr) 430 { 431 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 432 unsigned short usDspBaseIO = pSettings->usDspBaseIO; 433 bool bRC = 0; 434 435 if (pBDData->bDSPEnabled) { 436 switch (uOpcode) { 437 case IOCTL_MW_READ_DATA: 438 bRC = dsp3780I_ReadDStore(usDspBaseIO, pvBuffer, uCount, ulDSPAddr); 439 break; 440 441 case IOCTL_MW_READCLEAR_DATA: 442 bRC = dsp3780I_ReadAndClearDStore(usDspBaseIO, pvBuffer, uCount, ulDSPAddr); 443 break; 444 445 case IOCTL_MW_WRITE_DATA: 446 bRC = dsp3780I_WriteDStore(usDspBaseIO, pvBuffer, uCount, ulDSPAddr); 447 break; 448 } 449 } 450 451 return bRC ? -EIO : 0; 452 } 453 454 455 int tp3780I_ReadWriteDspIStore(struct thinkpad_bd_data *pBDData, unsigned int uOpcode, 456 void __user *pvBuffer, unsigned int uCount, 457 unsigned long ulDSPAddr) 458 { 459 struct dsp_3780i_config_settings *pSettings = &pBDData->rDspSettings; 460 unsigned short usDspBaseIO = pSettings->usDspBaseIO; 461 bool bRC = 0; 462 463 if (pBDData->bDSPEnabled) { 464 switch (uOpcode) { 465 case IOCTL_MW_READ_INST: 466 bRC = dsp3780I_ReadIStore(usDspBaseIO, pvBuffer, uCount, ulDSPAddr); 467 break; 468 469 case IOCTL_MW_WRITE_INST: 470 bRC = dsp3780I_WriteIStore(usDspBaseIO, pvBuffer, uCount, ulDSPAddr); 471 break; 472 } 473 } 474 475 return bRC ? -EIO : 0; 476 } 477 478